Skip to content Skip to sidebar Skip to footer

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

$('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

Solution 1:

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

Solution 2:

You can try this:

You must follow the following format

    $('element,id,class').on('click', function(){....});

*JQuery code*

    $('body').addClass('.anything').on('click', function(){
      //do some code here i.ealert("ok");
    });

Solution 3:

If you want to capture click on everything then do

$("*").click(function(){
//code here
}

I use this for selector: http://api.jquery.com/all-selector/

This is used for handling clicks: http://api.jquery.com/click/

And then use http://api.jquery.com/event.preventDefault/

To stop normal clicking actions.

Post a Comment for "$('body').on('click', '.anything', Function(){})"