PreventDefault () not working

My goal is to remove the check check action by clicking on its label Below is my code.

$('TABLE TBODY TR TD').each(function() { $(this).find('input').each(function() { $('label for='+$(this)+'').preventDefault(); }); }); 

Below is the relevant HTML ..

  <table> <tr><input type="checkbox" id="a1"><div><label for="a1">ClickMe</lable></div></tr> </table> 

Please help me.

+4
source share
2 answers

preventDefault not a method for the jQuery object itself. This is an event method that is passed to this event handler.

In addition, the syntax of the shortcut syntax is incorrect (you forgot the brackets and tried to associate a jQuery object with a string), and you are without the extra two nested each es.

it is better:

 $("table tbody tr td input").each(function () { $("label[for='" + this.id + "']").click(function (event) { event.preventDefault(); }); }); 

JSFiddle example: http://jsfiddle.net/s9D4n/

Perhaps even simpler, but admittedly not functionally equivalent:

 $("label").click(function (event) { event.preventDefault(); }); 
+4
source

You just need to remove the for attribute from the label.

 <label>ClickMe</label> 
+1
source

All Articles