robert wisyert
robert wisyert

Reputation: 65

Why `IE7 , 8` call function onchange immediately using javascript?

Why IE7 , 8 call function onchange immediately using javascript ?

When i test on other browser EG: firefox, chrome , when i checked or unchecked input type="checkbox". It's will be alert immediately. But on IE7 , 8 when i checked or unchecked input type="checkbox". I must to click page area again. It's will alert.

How to apply my code for IE7 ,8 will alert immediately.

http://jsfiddle.net/Fp4sJ/819/

<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
function number_fn()
{
    var number_val = document.getElementById('number').checked      
    alert(number_val);
}
</script>
<label style=" font-weight: normal; cursor: pointer;">
<input type="checkbox" style=" cursor: pointer; " id="number"  onchange="number_fn()" value="TICK"/> TICK 
</label>

Upvotes: 0

Views: 56

Answers (1)

Jonast92
Jonast92

Reputation: 4967

This is due to a bug with IE7 and IE8's change events.

Instead, you can use the onclick event listener:

<input type="checkbox" style="cursor: pointer;" id="number" onclick="number_fn()" value="TICK" />

Source.

Upvotes: 1

Related Questions