0

I have a JavaScript function which toggles a div to show/hide, it works if I just have the one link that toggles the single div. But I have a while loop which creates several of these links and div, one for each row. But only the first link works. I can add a $ at end of my id for both the toggle link and div, how do I tell JavaScript to look for this variable?

JavaScript:

$(function(){
    var id = document.getElementById();
    $('#toggleDiv').click(function(){
        $('#targetDiv').toggle();
    });
});

PHP:

echo "<a href=\"javascript:void(0);\" id=\"toggleDiv$ctr\">;
echo "<div id=\"targetDiv$ctr\" style=\"display:none;\">";

As you can see, the $ctr helps individualize each link and div, but I need java to identify which #toggleDiv is being clicked, allowing it to toggle the correct #targetDiv.

Any help?

0

3 Answers 3

1

'You don't need for all these elements to have their own IDs if you structure your html and jQuery selectors correctly; they can just all share classes.

Something like this could work in a parent/child set up:

 $(".question").click(function() {
    $(this).parent().children(".answer").toggle();
  });

PHP

echo '<div class="some-parent">';
echo '<a href="javascript:void(0);" class="question">question</a>';
echo '<div class="answer" style="display:none;">';
echo 'answer';
echo '</div>';

Here is a fiddle: http://jsfiddle.net/VPCcZ/1/

Or like this in a "next" situation:

jQuery('.question').click(function(){
    if ($(this).next().css("display") == "none") {
      $(this).next().attr('style', 'display: block !important');
    } else {
      $(this).next().attr('style', 'display: none !important');
    }
  });

PHP

echo '<a href="javascript:void(0);" class="question">';
echo '<div class="answer" style="display:none;">';
Sign up to request clarification or add additional context in comments.

4 Comments

why jQuery() and not the $() alias?
No good reason, copied from an no-conflict solution of mine. Fixed.
i tried your both your set up, but neither seem to work - should the "answer" div have to be within the parent div or outside?
@OmairVaiyani I've updated my answer with a small fix and a link to a working fiddle.
0

A naive and inefficient, but usable approach would be something like this:

var i = 0;
while ($('#toggleDiv' + i).length) {
    (function () {
        // Copy the iteration variable, otherwise all of the links will toggle
        // the *last* targetDiv instance!
        var copy = i;

        $('#toggleDiv' + copy).click(function () {
            $('#targetDiv' + copy).toggle();
        });
    })();

    ++i;
}

A better approach would be to tag all of the toggle link divs with the same CSS class, then apply the same click handler to all of them ($('.toggle-link').click(...)). This handler should look at the target element's ID and extract the number, then find the matching targetDiv instance and toggle it. With that approach, you would have exactly one event handler registered.

With the approach I've given in code, you will have one event handler for each toggle div and this may cause memory issues in the browser if you have thousands of such links. For a small number of links it's probably fine, but consider a different approach if you may wind up with many links.

Comments

0

Add a click event listener to all the links whose id attribute starts with toggleDiv. In the event handle function, extract the substring after toggleDiv, look for the div having the ID targetDiv<extracted_number>, and toggle it:

$('a[id^=toggleDiv]').click(function() {
    var id = $(this).attr('id');
    var number = id.substring('toggleDiv'.length);
    $('#targetDiv' + number).toggle();
});

1 Comment

The ID won't actually contain $ -- it is the beginning of the OP's PHP variable.

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.