How to start click function after element default behavior

My code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script src="jquery.js"></script> <script> $(function(){ $("body").on("click",".mes_sel",function(){ if($(".mes_sel:checked").length==0){ alert("Checked"); } else alert("Not Checked"); }); }); </script></head> <body> <input type="checkbox" class="mes_sel"> </body> </html> 

The warning is checked when the text field is disabled, since onclick is run before the check box is checked. How can I get the click event to fire after checking / unchecking input

0
javascript jquery html
Jun 22 '12 at 15:03
source share
3 answers

Use change to do this:

 $(function(){ $(".mes_sel").on("change", function(){ if($(this).is(':checked')){ alert("Checked"); } else { alert("Not Checked"); } }); });​ 

Demo

+1
Jun 22 '12 at 15:07
source share

Try this instead:

 $("body").on("click", ".mes_sel", function() { if ($(this).is(":checked")) { alert("Checked"); } else alert("Not Checked"); });​ 

JsFiddle example .

0
Jun 22 2018-12-18T00:
source share

I ran into this problem a while ago - I also found that different browsers handle this differently. Chrome was the worst criminal, if I remember correctly.

This is not necessarily the best solution, but you can attach each custom attribute to them, say "isChecked", and then drop your function from this attribute as follows:

 if ($("mes_sel").attr("isChecked") == "true") { ... } 

As I said, this is not the best solution, since you will have to manually turn the "isChecked" attribute to stay up to date with the actual value of the flag, but it will work if it is not more elegant for you.

If you need an example of this, check out this jsFiddle:

http://jsfiddle.net/NathanFriend/GAP2j/

This turns ordinary radio buttons into switch radio buttons - I was forced to use this special attribute method because browsers use onclick events in different ways.

0
Jun 22 2018-12-18T00:
source share



All Articles