1

This is my code:

<input type="text" id="x">
$(document).ready(function(){
    $("#x").onchange(function(){
        alert("x");
    })
});

But id doesn't work. What is problem?

3
  • try using keyup instead of onchange event Commented Jul 4, 2016 at 11:33
  • You need to get function call while each key up ? Commented Jul 4, 2016 at 11:36
  • .onchange is event of javascript. Change event in jquery is .change Commented Jul 4, 2016 at 11:37

4 Answers 4

2

change this:

$("#x").onchange

to this:

$("#x").change  

There is a .on() listner, so this can also be used:

$("#x").on('change', function(){
   alert("x");
})

You have a jQuery object and you have to bind the jQuery methods only. And change event on input[type=text] works when you off the focus out of it.

Sign up to request clarification or add additional context in comments.

3 Comments

the OP would still need to focus out of the textbox in order to get this working?
@AkshayKhandelwal seems due to error, nothing was happening, this should work. Although you are also correct, OP has to put focus out of the input to get it working.
OP did not ask to trigger change on keypress.
0

Use .change() event instead of .onchange() event in jquery.

$(document).ready(function(){
  $("#x").change(function(){
    alert("x");
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input type="text" id="x">

Comments

0

Because onchange is javascript. In jquery it's change

$("#x").change(function(){
                    alert("x");
                })

or

$("#x").on('change',(function(){
                    alert("x");
                })

But to trigger it you should change focus

Comments

0

$.onchange is not a function. You are getting confused between native JavaScript, which allows

<input onchange="doSomething()"/>

and jQuery, which requires

$(selector).on("change",function(){...});

The correct function is $.on(event) where event is a string. In this case you need

$("#x").on("change",function(){..});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.