Skip to content Skip to sidebar Skip to footer

Show/hide A Div On Click

i'm writing a small script to show/hide a div when other div is clicked, but I can't get the second div clickable. Here's my code so far: $(document).ready(function(){ $('div#o

Solution 1:

Why do you want to select your element with next if it has an unique ID?

$(document).ready(function(){
    $('div#ordontia').click(function(){
        $('div#ordontia2').slideToggle("slow");
    });
});

more general if you add more divs:

$(document).ready(function(){
    $('.botaomedicina').click(function(){
        $('#'+$(this).attr('id')+'2').slideToggle("slow");
    });
});

with all others closing:

$(document).ready(function(){
    $('.botaomedicina').click(function(){
        $('.botaomedicinadescription').slideUp("slow");
        $('#'+$(this).attr('id')+'2').slideToggle("slow");
    });
});

Solution 2:

Don't use $.next, it only selects siblings of the current element:

Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector. — jQuery documentation: .next()

Use the normal one:

$('div#ordontia2').slideToggle("slow");

Solution 3:

Fixed it.

http://jsfiddle.net/65AK2/2/

firstly, it lookx like your toggled div was mal-formed. I didnt see a for it.

Secondly, if you know what the ID of the other div is, you dont need to say:

$(this).next("#item");

, it would make no sense.

Solution 4:

$(document).ready(function(){
    $('div#ordontia').click(function(){
        $('div#ordontia2').slideToggle("slow");
    });
});

remove this ;)

Solution 5:

try this

$(document).ready(function(){
    $('div#ordontia').click(function(){
        $('div#ordontia2').slideToggle("slow");
    });
});

updated fiddle http://jsfiddle.net/65AK2/4/

Post a Comment for "Show/hide A Div On Click"