JavaScript: Listen for changes

Basic

In HTML

[js]

[/js]

In JavaScript

[js]
object.onchange = function(){myScript};
[/js]

In JavaScript, using the addEventListener() method:

[js]
object.addEventListener(“change”, myScript);
[/js]

Specific cases

Detect change on textbox’s content without using keyup because keyup also detects keystrokes which do not generate letters, like the arrow keys
[js]
jQuery(‘#some_text_box’).on(‘input’, function() {
// do your stuff
});
[/js]
or
[js]
jQuery(‘#some_text_box’).on(‘input propertychange paste’, function() {
// do your stuff
});
[/js]

Change tab order of other elements when tabbing from an element

[js]
document.getElementById(‘current_element’).onkeydown = function (e) {
if (e.keyCode == 9)
{
document.getElementById(“otherElem1”).tabIndex = 11;
document.getElementById(“otherElem2”).tabIndex = 12;
document.getElementById(“otherElem3”).tabIndex = 13;
}
}
[/js]
or
[js]
document.getElementById(‘current_element’).addEventListener(‘keyup’, function (e) {
if (e.keyCode == 9) {
document.getElementById(“otherElem1”).tabIndex = 11;
document.getElementById(“otherElem2”).tabIndex = 12;
document.getElementById(“otherElem3”).tabIndex = 13;
}
}, false);
[/js]

Be the first to comment

Leave a Reply

Your email address will not be published.


*