two functions within the onchange

Asked

Viewed 3,520 times

5

Colleagues.

I’m trying to put two functions inside the onchange, but the functions only work when shooting one or the other.

onchange="soma(); alterar();"

How could I fix this?

Thank you!

1 answer

14


The best way would be to use .addEventListener thus

el.addEventListener('change', soma);
el.addEventListener('change', alterar);

Another option would be to pass a function that calls both:

onchange="processar();"

and

function processar(){
    soma();
    alterar();
}

If these two functions have common logic it would be better to have one call to the other.

Another option is to put all actions separated by commas for Javascript to run all:

onchange="(soma(), alterar());"

Example:

Clica aqui:
<input type="checkbox" onChange="(alert(1), alert(2));">

  • 1

    Thank you Sergio.

  • 1

    In older versions of IE the addEventListener does not work, has to use the attachEvent. The attachEvent expects events with the prefix on. Suggestion: if (el.attachEvent) { el.attachEvent("onchange", foo, true); } else { el.addEventListener("change", foo); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.