1

I want to clear out a textbox when I call:

$('#textBox').val(''); or $('#textBox').empty();

Originally, when I would manually backspace and delete the text out of the text box, the on change function would fire. If I run $('#textBox').val('') above, on change does not fire:

$(document).ready(function() {
    $('#textBox').on('change', function(){

Why is this?

1
  • your change event will not fire if you clear textbox using jquery. Commented Jan 17, 2014 at 4:34

3 Answers 3

4

Use trigger, like this:

$("#testBox").val("").trigger("change");
Sign up to request clarification or add additional context in comments.

Comments

1

When you change the value programmatically, just trigger the change event using trigger.

$("#myid").trigger("change");

Comments

1

It's not jQuery that doesn't fire the event, it's the browser. If you look it up in the specs you'll see the definition of the change event:

change

The change event occurs when a control loses the input focus and its value has been
modified since gaining focus. This event is valid for INPUT, SELECT, and
TEXTAREA. element.

    Bubbles: Yes
    Cancelable: No
    Context Info: None

As already posted, your problem can be mitigated by triggering the event manually via trigger().

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.