views:

558

answers:

6

I have the following code

$(document).ready(function() {
  $('a#slick-toggle').click(function() {
 $('#slickbox0').toggle(400);
 return false;
  });
});


<a id="slick-toggle" href="#">Show/Hide</a>

<div id="slickbox" style="display: none;">
hidden content here
</div>

The problem is, if there is more than 1 instance of this on the html part of this on the page, it wont work. Im not very good at JS. The div id I gotta make unique.... but how do I make each link toggle the corresponding box?

+3  A: 

You shouldn't repeat IDs in your HTML. But you do want to use the same functionality in jQuery, so add a common class name so that you can apply the function to all of these anchors.

Also, since you're not sure about whether the HTML structure can be predictable, you'll need to output a link between anchor element and div element. Note the data-id attribute below:

<a id="someID" class="slick-toggle" data-id="334" href="#">Show/Hide</a>

<div id="someOtherID" class="slickbox" data-id="334" style="display: none;">
  hidden content here
</div>

This "334" could be anything. This would be something unique that you can decide on when you're outputting the content.

Now your code can target each .slick-toggle class, and have it toggle the associated div.

$(document).ready(function() {
    $('a.slick-toggle').click(function() {
        var dataID = $(this).attr("data-id");
        $("div[data-id=" + dataID + "].slickbox").toggle(400);
        return false;
    });
});
bigmattyh
Problem with this.... if there is other code between the hide/show link and the div, it doesnt work.
Yegor
Since you say you're not so good at javascript, I will add some explanation to this entry. bigmattyh adds a class to both the anchor s well as to the div (if you want you can still add unique id's to all of them). He looks for whenever a link with that specific class is clicked, and then in the event looks for the next sibling of that link that is a div with the class 'slickbox'. So the class-name is a common property to all items that you want to handle in such way.
Gidon
The point is, use a pattern that you can follow. If you don't know for sure what the HTML is going to be, you'll need to create a specific link between your toggle button and the content, when you output the page.
bigmattyh
Yegor: it will find the first div.slickbox, if that expression only return the very next element, he can use .sibling('div.slickbox:first'), which will query all the siblings and return the first div.slickbox.
Gidon
I've updated the post to allow for arbitrary HTML in your document structure. You do have to link the two with some associated id though. Just output something unique to the toggle link/toggle box combination, and put it in the data-id attribute of the anchor and div elements.
bigmattyh
A: 

You could set up naming convention between your link and corresponding div.

<a id="toggle-box1" class="toggler">click/show</a>
<div id="box1">hide me</div>

And then set them up generically:

$(".toggler").click(function() {
 var targetId = $(this).attr("id").replace("toggle-", "");
 $("#" + targetId).toggle(400);
});
Rashack
A: 

Not tested, but this uses .each and contextual selectors:

<div class="group">
    <a class="toggle" href="#">toggle</a>
    <div class="content-hidden">content!</div>
</div>

<div class="group">
    <a class="toggle" href="#">toggle</a>
    <div class="content-hidden">content!</div>
</div>

<div class="group">
    <a class="toggle" href="#">toggle</a>
    <div class="content-hidden">content!</div>
</div>

<style>
div.content-hidden { display:none; }
</style>

<script>
$('.group').each(function() {
    var toggle = $('.toggle', this), content = $('.content-hidden', this);
    toggle.click(function(e) {
        e.preventDefault();
        $(content).toggle();
    });
});
</script>
meder
A: 

I asked a very similar question just a few days ago, as I had the same problem. It may help you! Lots of sample code is provided.

Maxim Zaslavsky
A: 

You need to connect the show/hide link with the appropriate box somehow. I would suggest using something like this:

<a class="slick-toggle" href="#slickbox1">Show slickbox 1</a>

<div class="slick-box" id="slickbox1">
 Hidden content here
</div>

<a class="slick-toggle" href="#slickbox2">Show slickbox 2</a>

<div class="slick-box" id="slickbox2">
 Hidden content here
</div>

<a class="slick-toggle" href="#slickbox3">Show slickbox 3</a>

<div class="slick-box" id="slickbox3">
 Hidden content here
</div>

<script type="text/javascript">
 jQuery(function(){
  $('a.slick-toggle').click(function(){
   $($(this).attr('href')).toggle(400);
  });
 });
</script>

This way each link's href attribute connects it to the appropriate div to toggle.

donut
A: 

The bigmattyh method works fine for me!!! Thanks a lot!

Espai Píxel