3

if i have a script but it needs to run multiple times on a page, like in a cms for example, how do you approach this? in one experiment i had the code run multiple times but i put the article id on the end of the selectors that would fire off commands and what needed to be manipulated. it's not a good workaround though cause there's too much duplication of code (even though it works).

here is the example that i got help with in a recent stack overflow discussion (with the article ids appended(textpattern)):

<script type="text/javascript">
    $(document).ready(function() {
        $('.fullTracksInner<txp:article_id />').hide();
        $('.tracklist<txp:article_id />').click(function() {
            $('.fullTracksInner<txp:article_id />').slideToggle('medium');
            if ($('.fullTracksInner<txp:article_id />').is(':hidden')) {
                $(this).text('Show Tracklist');
            } else {
                $(this).text('Hide Tracklist');
            }
        });
    });
</script>

just imagine for example three slideshows on a page using the same slideshow script.

3
  • 2
    code beautification courtesy of jsbeautifier.org Commented Jul 26, 2010 at 2:44
  • You may be able to generalize the script but maintain the functionality for each independently if they share a common structure or class setup. Can we see your html? Commented Jul 26, 2010 at 3:00
  • sure, the html that is being affected looks like: <div class="fullTracks"> <div class="fullTracksInner<txp:article_id />"> <p><txp:body /></p> </div> </div> <!-- end fullTracksInner class --> <p><a href="#activity" class="tracklist<txp:article_id />">View Tracklist</a></p> Commented Jul 26, 2010 at 3:15

2 Answers 2

2

This is a relatively common task to do in jQuery. In order for this to work for multiple elements on the same page without requiring unique IDs, you just need to use $(this) in order to define the relative element you're acting on. I don't know what you're markup looks like, but you could probably do something like the following:

$(document).ready(function() {
 $('.fullTracksInner<txp:article_id />').hide();

    $('.tracklist<txp:article_id />').click(function() {
     $(this).children('.fullTracksInner<txp:article_id />').slideToggle('medium');
        if ( $(this).children('.fullTracksInner<txp:article_id />').is(':hidden') ) {
            $(this).text('Show Tracklist');
        } else {
            $(this).text('Hide Tracklist');
        }
    });
});

You should probably modify your selectors a little though, I would think that $('.tracklist<txp:article_id />') might choke in some browsers.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks this looks better, by adding in more use of $this as for the selectors, the txp:article_id thing just becomes a number after the cms parses it.
I don't fully understand how your CMS parses the code, but for your selector, I would recommend trying something like $("div[class^='tracklist']");
0

You could extract the repeating code to standard JavaScript methods and then call them wherever desired.

<script type="text/javascript">
function performSlide(tracklist) {
    var fulltracks = $('.fullTracksInner<txp:article_id />');

    $(fulltracks).slideToggle('medium');
    if (fulltracks).is(':hidden')) {
        changeText(tracklist, 'Show Tracklist');
    } else {
        changeText(tracklist, 'Hide Tracklist');
    } 
}

function changeText(textbox, text) {
    textbox.text(text);
}

$(document).ready(function() {
    var tracklist = $('.tracklist<txp:article_id />');

    tracklist.click(function() {
        performSlide(tracklist);
    });

    $('.someRadioButton').change(function() {
        performSlide(tracklist); 
    });
});
</script>

1 Comment

thank you - this probably wouldn't get around the problem though of having to make unique IDs for every instance. i saw this thread come up on the sidebar which might be more along the lines of what i'm wondering about: stackoverflow.com/questions/2082218/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.