Annie The Cross Annie The Cross - 3 months ago 8
HTML Question

Click function on class

how can I improve my script if I want to on click on h3 show p - but I have more of these elements and they open all on click. Is it necessary to give all h3 id?

$('.question h3').click(function(){
$('.question p').toggle();
});

Answer

Can you try like this:

https://jsfiddle.net/ay6xocLx/1/

 $('.question h3').click(function(){
    $(this).next('p').toggle();
 });

via $.next() we are targeting next DOM element.

Comments