开发者

jQuery: Select elements with Incrementing ID names?

and thanks in advance for your help!

Here's my situation: I have a set of divs whose IDs have an incrementing number applied to the end of the name using PHP. Each of these divs are added dynamically with PHP (They are a series of FAQ question开发者_开发技巧s with a hidden div container with the answers, that slide down when the question is clicked.) [Live Example][1]

There is no limit to the number of questions that appear on the page, because this is being used for a Wordpress theme and my client wants to add new questions as they go along.

Here's an example of the structure for each FAQ question using the PHP:

<?php var $faqnum = 0; $faqnum++; ?>
<div id="faqwrap<?php $faqnum; ?>">   
    <h4><a href="#faq<?php $faqnum; ?>" id="slidetoggle">What data is shared?</a></h4>   
     <div id="faqbox<?php $faqnum; ?>" class="slidebox">
        <p>Data sharing is defined by the type of service:</p>
        <ul class="list">
            <li>Third-party access to data (Enhanced Services only is strictly controlled and determined by the SDA)</li>
            <li>All members must participate in points of contact and conjunction assessment but can choose whether to participate in other services</li>
            <li>Participation in a service requires the member to provide associated data<br />
      </ul>
    </div>
    </div>

Now this is what I have currently in jQuery, and it works, but only if I add a new one every time my client wants to add a new question.

$(document).ready(function() {
 $('.slidebox*').hide();

// toggles the slidebox on clicking the noted link
   $("#faqwrap1 a:not(div.slidebox a)").click(function() {
      $("#faqbox1.slidebox").slideToggle('normal');
      $('div.slidebox:not(#faqbox1)').slideUp('normal');

return false;
     });
});

I thought of maybe doing something with a declared variable, like this:

for (var x = 0; x < 100; x++;) {
$('#[id^=faqwrap]'+ x 'a:not(div.slidebox a)')...
}

I hope this is clear enough for you! Again, I thank you in advance. :)


The best way to handle this is to not use the IDs, but use classes for the outer element. So your PHP would be altered like this:

<?php var $faqnum = 0; $faqnum++; ?> 
<div id="faqwrap<?php $faqnum; ?>" class="question">    
    <h4><a href="#faq<?php $faqnum; ?>" class="slidetoggle">What data is shared?</a></h4>    
    <div id="faqbox<?php $faqnum; ?>" class="slidebox"> 
        <p>Data sharing is defined by the type of service:</p> 
        <ul class="list"> 
            <li>Third-party access to data (Enhanced Services only is strictly controlled and determined by the SDA)</li> 
            <li>All members must participate in points of contact and conjunction assessment but can choose whether to participate in other services</li> 
            <li>Participation in a service requires the member to provide associated data<br /> 
       </ul> 
    </div> 
</div> 

Your JQuery would be rewritten with the selector for the class "question".

$(document).ready(function() {      
 $('.slidebox*').hide();      

 // toggles the slidebox on clicking the noted link      
 $(".question a:not(div.slidebox a)").click(function() {
    /* close everything first */
    $('div.slidebox').slideUp('normal');      
    /* selects and opens the the slidebox inside the div */
    $(".slidebox", this).slideToggle('normal');

    return false;      
 });      
});      

This will get you the effect you are looking for. The key differences in the JQuery is the way you get the slidebox inside the question that got clicked. I'm using the scoped selection $(".slidebox", this) to get just the slidebox inside the clicked ".question" element.

The subtle visual difference is that the slideUp() happens before the slideToggle(). This will essentially close any open queries before it opens the desired one. If you keep your animations fast, this will be more than fine. The advantage of this approach is that you don't have to worry about the count of questions on a page, and the selectors are most likely more optimized than the for loop.

Edit I adjusted the PHP code to use a class for "slidetoggle" instead of an id. It's technically an HTML error to have multiple IDs that are the same. It can throw off some assistive technologies for people with dissabilities. I'm assuming that section of code was repeated several times on the page.


Without changing your current markup, this would work:

// toggles the slidebox on clicking the noted link
$("div[id=^faqwrap]").each(function () {
  var $faqwrap= $(this);
  $faqwrap.find("h4 > a").click(function () {
    var $currentSlidebox = $faqwrap.children(".slidebox");
    $currentSlidebox.slideToggle('normal');
    $('div.slidebox').not($currentSlidebox).slideUp('normal');
    return false;
  });
});

Maybe you can find a few suggestions in the above code that help you.

Like @Berin, I'd also recommend giving a separate CSS class to the outer DIV and using that as a selector, instead of $("div[id=^faqwrap]").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜