Nate Pet
Nate Pet

Reputation: 46222

Jquery .on() not working

I have the following:

<input type="submit" id="submit" value="Submit" />

Inside of jQuery I have have the following:

<script type="text/javascript">
$(document).ready(function () {
    $("#submit").on("click", function () { 
        alert('test');
    });
});
</script>

This doesn't work. I am using IE9.

Might anybody know which version of Jquery is supporting .on()?

Upvotes: 6

Views: 8183

Answers (6)

josh1978
josh1978

Reputation: 738

I encountered this using when using IE9 in compatibility mode. Note, I had no problem when using IE9 in regular mode.

I was able to do a more thorough QA for a site I just launched (time didn't permit much cross-browser testing beyond using a screenshot service). Changing $("something").on("click"...) to $("something").click() fixed the issue.

Upvotes: 0

Jeremy
Jeremy

Reputation: 81

Make sure that the 'submit' element you are referring to actually exists at document load. If you are creating it dynamically, the 'on' function will have nothing to bind it to.

Upvotes: 0

Codrin Eugeniu
Codrin Eugeniu

Reputation: 1383

IE doesn't do well when using 'reserved' names as element's ID.

See this fiddle, your code works great if you change the ID of the input to something different than '#submit'

http://jsfiddle.net/6yAuz/3/

Upvotes: 1

Usman Chaudhry
Usman Chaudhry

Reputation: 430

Try following alternates:

 $('#submit').bind('click', function() {
  alert('Test');
});

OR

$("#submit").click(function(){
   alert("Test");
});

Upvotes: 1

Shyju
Shyju

Reputation: 218732

Use this version of on

$(function(){
   $(document).on("click","#submit", function (e) { 
    //e.preventDefault();  // if you want to prevent default form submission
     alert('test');
   });
});

Upvotes: 6

mgraph
mgraph

Reputation: 15338

make sure you load jQuery.js then try:

$("#submit").click(function(){
   alert("test");
})

Upvotes: 2

Related Questions