[ACCEPTED]-Javascript Textbox Event-textbox

Accepted answer
Score: 12

use onchange instead of onkeyup in this case

see: http://www.w3schools.com/jsref/event_onchange.asp

e.g.

<input type='text' value='abc' onchange='changeSomething(this);' />

to 14 get around this

EDIT Two things:
1) Autocomplete 13 values can be selected using arrow keys 12 and enter/tab, and by using the mouse. The 11 arrow keys/enter.tab fire the onkeyup events... clicking 10 in the autocomplete box does not, however, fire 9 the onclick event.
2) The onchange event 8 fires as soon as focus is lost IF the content 7 has changed. Focus is not lost when selecting 6 autocomplete values.

Essentially, there does 5 not appear to be any way to reasonably guarantee 4 the event will be processed the way you 3 want.

First off, do you really need to listen 2 to every keystroke? Secondly, would you 1 be better served by turning off autocomplete? (e.g. <input type='text' value='abc' autocomplete='off' onkeyup='changeSomething(this);' />)

Score: 3

Here's a solution which polls the element 1 periodically for any changes

<script type="text/javascript">

var changeWatcher = {
    timeout: null,
    currentValue: '',
    watchForChange: function( el ) {
        if( el.value != this.currentValue ) {
            this.changed( el );
        }
        this.timeout = setTimeout( function() {
             changeWatcher.watchForChange(el)
        }, 200 );
    },
    cancelWatchForChange: function() {
        clearTimeout( this.timeout );
        this.timeout = null;
    },
    changed: function( el ) {
        this.currentValue = el.value;
        // do something with the element and/or it's value
        //console.log( el.value );
    }
}

</script>
<input type='text' value='abc' onfocus='changeWatcher.watchForChange(this)' onblur='changeWatcher.cancelWatchForChange()' onchange='changeWatcher.changed(this)' />

More Related questions