Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$('body').on('click', '.anything', function(){})

Tags:

$('body').on('click', '.anything', function() {     //code }); 

doesn't work for anything right now and I can't figure out why. I'm able to anchor to anything else, say I just toss a #wrap div right inside the body. Then I'm able to do

$('#wrap').on('click', '.anything', function() {     //code }); 

for any element I want.

Any idea what I could have done to disable this ability on the body element?

Thanks!

like image 969
fancy Avatar asked Aug 12 '12 10:08

fancy


1 Answers

You should use $(document). It is a function trigger for any click event in the document. Then inside you can use the jquery on("click","body *",somefunction), where the second argument specifies which specific element to target. In this case every element inside the body.

$(document).on('click','body *',function(){     //  $(this) = your current element that clicked.     // additional code }); 
like image 183
Syed Arif Iqbal Avatar answered Oct 25 '22 22:10

Syed Arif Iqbal