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.

demo

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'); } 

fiddle 1

if, instead, want show 1 of p elements, define this:

function classchange() {   $('.active').removeclass('active');   $(this).next('p').addclass('active'); } 

fiddle 2


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -