2

I have an html element that is suppose to call 3 JS functions as its onchange event, however; it only fires off the first function. I am stumped as to why it is not doing it.

<select id="dlist" onChange="func1(); func1(); func3()"/>

func1 = function(){
//code
}

func3 = function(){
//code
}

func2 = function(){
//code
}

What could this issue be?

MasterP
  • 628
  • 2
  • 13
  • 23
  • Works fine for me: http://jsfiddle.net/BjbDg/. It calls `func1` and `func3`, just like you have it in the attribute (note that you are calling `func1` **twice**). – Felix Kling Aug 08 '12 at 14:04
  • One reason why the other methods are not called could be that you have an error in `func1` which terminates the execution. But that's just guessing. As you can see in my example, if I take the code as you posted it, it calls the functions. – Felix Kling Aug 08 '12 at 14:19

3 Answers3

3
<select id="dlist" "onChange=func1(); func1(); func3()"/>

You are wrapping the entire attribute in quotes. You want

<select id="dlist" onChange="func1(); func1(); func3()"/>
Pekka
  • 431,103
  • 135
  • 960
  • 1,075
1

Try onChange="func1(); func1(); func3()".

Move the quote.

Igor
  • 31,955
  • 14
  • 75
  • 111
Vic
  • 7,123
  • 5
  • 39
  • 51
-1

You can try this

var slt= document.getElementById('dlist').onchange= func1; func1;func3

or

var slt= document.getElementById('dlist').onchange= myAll;


function myAll(){

func1();
func2();
func3();

}
Jitender
  • 7,057
  • 25
  • 92
  • 193
  • That's just completely wrong. This assigns the return value of `func1` to `.onchange`. – Felix Kling Aug 08 '12 at 14:05
  • This will now assign `func3` to `.onchange`... slightly better, but only `func3` will be called when the event is triggered, not all three functions. *edit again:* This will assign `func1` to `.onchange`, so only `func1` will be called when the event is triggered (referring to the first snippet). – Felix Kling Aug 08 '12 at 14:10
  • The second one will work, but you don't have to create a named function. You could do `el.onchange = function() { funct1(); func2(); funct3(); };`. The first one (and everything else before) is (logically) incorrect. – Felix Kling Aug 08 '12 at 14:17