4

I would still take advantage of the expertise and availability of this community.
My question is: why if we have a code similar to

<input name="" type="checkbox"  onClick="first()" onChange="second()">
<script type="text/javascript">
function first(){
    console.log('first')
    }
function second(){
    console.log('second')
    }
</script>

in firefox onclick event fires before onchange while in chrome happens the opposite?
Thanks for your answers, maybe i have not explained in the best way
I know that using onMuseDown the problem is solved but i want to understand the reason for such behavior in Chrome with the onchange and onclick event

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Devima
  • 1,566
  • 2
  • 10
  • 16
  • Why do you want to use them both? I prefer onClick on checkbox for cross browser consistency. The order depens on browser implementation see also: http://stackoverflow.com/questions/5575338/what-the-difference-between-click-and-change-on-a-checkbox – Irvin Dominin Apr 23 '14 at 11:20
  • Chrome will simply fire the event in the order it encountered it, doing `onChange="second()" onClick="first()"` will reverse it. – Kyle Needham Apr 23 '14 at 11:20
  • @KyleNeedham: thank you for the answer but also if i reverse the events in my chrome version the result is still the same – Devima Apr 23 '14 at 11:25

2 Answers2

1

Because onChange is triggered due to onClick

Shijin TR
  • 7,516
  • 10
  • 55
  • 122
1

One dirty solution might be to use onMouseDown instead of onClick.

But I'm also wondering why you really need both ;)

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
Vincent
  • 620
  • 7
  • 19
  • I know that with onMouseDown the code works, but i want to understand the reason this behavior, thanks for the answer. – Devima Apr 23 '14 at 11:27