stefan bachert
stefan bachert

Reputation: 9626

onchange hijacks onclick

Modifying the text and then clicking on the button triggers only the onchange code

But I need to know whether the button has been clicked:

<input type="text" onchange="alert('change')" value="Text">
<input type="button" onclick="alert('click')" value="Button">

What do I need to change to get the click handler?

Upvotes: 0

Views: 783

Answers (2)

lostsource
lostsource

Reputation: 21840

The alert comes up between the onmousedown and the onmouseup events, causing an onclick to never trigger

Upvotes: 0

Alex Wayne
Alex Wayne

Reputation: 187204

That works fine, it's alert() that sucks.

I don't know the exact details around it, but I've noticed in the past that alert() can mess with DOM events in strange ways when those events would occur together.

<input type="text" onchange="console.log('change')" value="Text">
<input type="button" onclick="console.log('click')" value="Button">​

See here: http://jsfiddle.net/25EsQ/ (Make sure to bring up the JS console so you can see the output)

The good news is this is a debugging issue only (hopefully) and when you use real and useful JS code instead, it should work as you expect.

Upvotes: 2

Related Questions