How to distribute a child from a click event

Can someone help me with this. HTML code:

<h3> <label> <input type="checkbox" name="country" value="us" /> United States </label> </h3> <p>Some content goes here</p> 

I want to switch the p element by clicking on the h3 tag, but I don't want to switch if I clicked on the / strong>

 $('h3').click(function() { // Does something goes here? $(this).next('p').toggle(); } 
+4
source share
2 answers

You need to check the purpose of the action.

 $('h3').click(function(e) { // if they clicked the h3 only if (this == e.target) { $(this).next('p').toggle(); } } 

The altCognito clause will also work, but this is more code.

+17
source

Do you want to stop Propagation ()

 $('h3').click(function() { // Does something goes here? $(this).next('p').toggle(); } $('label').click(function(e) { e.stopPropagation(); } 
+2
source

All Articles