Add multiple onchange functions from javascript to one input

I want to add several onchange functions from javascript to the same input. some such as:

this.el = document.getElementById(this.docID);

if(x==y)
{

 this.el.onchange += function(){ // some code }

}
if(a==b)
{

 this.el.onchange += function(){  // some code }

}
+3
source share
4 answers

If you want to use the onchange functions, you can do something like this:

this.el = document.getElementById(this.docID);

if(x==y) {
  this.el.onchange = function(){ // some code }
}

if(a==b) {
  var oldOnChange = this.el.onchange;
  this.el.onchange = function(){ 
    // execute previous onchange function
    if(oldOnChange) oldOnChange();

    // some code 
  }
}

Similarly, you can also decide whether you want the old exchange function to be executed before or after the new exchange code.

+2
source

onchange addEventListener DOM2. ( onchange), , jQuery change .

jQuery, :

this.el = document.getElementById(this.docID);

if(x==y) {
    $(this.el).change(function() { /* some code */ });
}
if(a==b) {
    $(this.el).change(function() { /* some code */ });
}
+2

2 , . 1 .

, - :

this.el.onchange = function() {   
    if(x==y) {
        // some code
    } else if(a==b) {
        // some other code  
    }  
}
0

this.el.onchange , ,

this.el = document.getElementById(this.docID);

this.el.onchange += functionx();

functionx() {

if(x==y) {

this.el.onchange += functionY(){ // some code }

} if(a==b) {

this.el.onchange += functionZ(){ // some code }
}

}

-1

Source: https://habr.com/ru/post/1751723/


All Articles