Skip to content

Instantly share code, notes, and snippets.

@brandonaaskov
Created January 11, 2012 21:34
Show Gist options
  • Save brandonaaskov/1596867 to your computer and use it in GitHub Desktop.
Save brandonaaskov/1596867 to your computer and use it in GitHub Desktop.
jQuery's change() event doesn't work as expected with input text fields. This, however, does work.
/*
For some reason, the change() event only fires when the input field loses focus.
Binding to other options ('change keypress paste focus textInput input') will
fire the event several times, which is bad. The below code works even when
content is pasted into the text field, and only fires once as expected.
*/
$('#search-form .term').bind('input', function(){
console.log('this actually works');
});
/*
HTML example:
<form id="search-form">
<input type="text" class="term" name="Search" value="Search" autocomplete="off" />
<input type="submit" name="Search" value="Search" />
</form>
*/
@alextosic
Copy link

Didn't even know there was an 'input' event. Thanks a lot.

@budkin76
Copy link

Excellent solution and exactly what I needed. Thank you!

@wytrych
Copy link

wytrych commented Sep 27, 2016

Better to use .on('input') as bind is a core javascript function https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

@saclarky
Copy link

saclarky commented Dec 9, 2016

so helpful!

@tolgaparlan
Copy link

Saved me a lot of time. Thx!

@dagger2014
Copy link

I was struggled with onchange event and this is great help.

@ufrik
Copy link

ufrik commented May 3, 2017

Thank you very much. This works perfectly with Google Chrome for mobile.

@weeco
Copy link

weeco commented May 19, 2017

Simply perfect!

@athenaozanich
Copy link

Perfect! Thanks for the insight!

@vkotek
Copy link

vkotek commented Apr 2, 2018

Cheers! Works as expected.

@jeremylangerpersonal
Copy link

Excellent, works perfectly, thank you!

@rlugge
Copy link

rlugge commented May 29, 2018

Thank you for the save!

@swapnilbanga
Copy link

Thanks!

@wdburgdorf
Copy link

Tried all versions I found in this thread, but for +/- buttons, but ...
$('.theclass').change('input', function(){
console.log('this actually fails');
});
... only works when changing the value by typing. But changing by clicking +/- buttons does nothing. Maybe a conflict with another function that is called when the value changes?

@jpausa
Copy link

jpausa commented Jun 22, 2018

I'm trying it to use with a DB of users, but it take so long to respond and show the result. Any idea please!!!!

@bastiendmt
Copy link

@jpausa First, test your request with your database and see the time used by that request to execute. Then try to simplify as mush as possible your request, select only the field that are required. I am getting good response time with my code.

@Budapresst
Copy link

thanks for this!

Copy link

ghost commented Nov 14, 2018

Thank you.

@muhammedbalta
Copy link

Thank you.

@rodrigoramirez93
Copy link

Thanks!

@Fausto4911
Copy link

It is work as expected, Thanks!!!

@dsbilling
Copy link

Thanks :)

@brains1960
Copy link

Thank you

@umapatitripathi
Copy link

thanks this work really !!

@umapatitripathi
Copy link

it works as...

        $('#xyz').bind('input', function () {
            alert('Changed!');
        });

// -- where "xyz" is the text box id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment