javascript - Select the direct next of a certain element -
i trying add class next p element after item clicked. in function part removes class working selector p element not correct nothing happening that.
function classchange() { if($(this).hasclass('active')) { //do nothing } else { $('.active').removeclass('active'); $(this).next('p').addclass('active'); } } $('h1').click(function() { classchange(); });
in classchange() function, this refers window object (assuming function in global scope).
if want refer clicked h1 element, define click event this:
$('h1').click(classchange); if want toggle next p element's display, define classchange() this:
function classchange() { $(this).next('p').toggleclass('active'); } if, instead, want show 1 of p elements, define this:
function classchange() { $('.active').removeclass('active'); $(this).next('p').addclass('active'); }
Comments
Post a Comment