tags:

views:

46

answers:

1

I'm using this jquery code to show and hide divs on my page. As you can see, I an limited to 5 divs. Is there a way to make them dynamic?

$(document).ready(function() {
  $('.box').hide();
  $('a.toggle').click(function() {
    $('.box').toggle(400);
    return false;
  });
});


<a href="#" class="toggle" title="Show Comments"><img src="images/read.gif" alt="" /></a>
<div class="box" class="comment">hidden content</div>

This is my revised code

$(function () {
  $('div.box').hide();
  $('a.toggle').click(function () {
    $(this).next('div.box').toggle(400);
  });
});


<div class="entry">
  <h3>Title</h3>
  <p>content</p>
  <p class="posted">Posted by Admin<br />
  <a href="#" class="toggle">
    <img src="read.gif" alt="" />
  </a>
</div>
<div class="box" class="comment">Hidden</div>
+3  A: 

Looking at your markup, you could do something like this:

$(document).ready(function () {
  $('div.box').hide();

  $('a.toggle').click(function () { // all A elements with class 'toggle'
    $(this).next('div.box').toggle(400); // toggle the next DIV with class 'box'
  });
});

Basically bind a click handler to all your links with class toggle, and then when they are clicked, it will look for the next sibling (relative to the clicked link) that is a div with class box, using the Traversing/next function.

Check the above example with your markup here.

Edit: I reviewed your markup, and your .toggle links are nested inside a div.entry element, also there is an unclosed paragraph, so I've adjusted the code to your markup again:

$(function () { 
  $('div.box').hide(); 
  $('a.toggle').click(function () { 
    $(this).parents('.entry').next('div.box').toggle(400);
  }); 
});

You will notice that I look for the div.entry element, since the .box are siblings of them.

Check an update example here.

CMS
Hi CMS, thanks for the help. I have absolutely no javascript/jquery knowledge at all. Is there an example that you can point me to so that I can follow what your talking about? One of the issues that I see in my markup is that I have 2 classes and that breaks things. Is this allowable to have 2 classes within the markup?
Jack
CMS, thanks for the example. I really appreciate it. Unfortunately, I cannot get this working. WHen I click the button, nothing happens. I will post my revised code above.
Jack
Thanks again for the effort CMS. It won't work. What I did was copy out your exact code and paste it into a new page. Even with nothing else other than your code and of course the jquery bin, it won't work. Any ideas?
Jack
I tried your second example and it works within your example. I cannot however get this to work in my own markup. Let me ask you a question. In your second example, I attempted to add a second link with a second hidden box and when I click on the second link, the first box shows. Is this script dynamic in the sense that it is smart enough to know which link coorasponds to the correct hidden box?
Jack
Well, I'm not sure what's happening with the rest of the code you have, could you post a complete non-working example at http://jsbin.com ?
CMS
Disregard my last comment CMS. I got your example to work. Even though I don't yet have it working in my form, I should be able to take it from here. Thank you very much!
Jack
CNS, are you still around? I think I see why it isn;t working in my form. In your example, your hidden div is below the div that holds the anchor tag. In my markup, my hidden content in nested within the same "entry" div. I realize that that was how I originally posted it and I'm sorry for that but I had to dig it out of my PHP code and misjudged it. How might this change the jquery code?
Jack
Instead of `.next` use `.children` or `.find`
fudgey