The following code from your current example:
<input id="i" type="text" autofocus onfocus="alert(1)"> <script type="text/javascript"> document.getElementById('i').addEventListener('focus', function() { alert(2); }, false); </script>
Will trigger an endless loop of warnings going from 1 to 2
[Eidt]
because: (this only happens in browsers supporting autofocus )
the input receives autofocus, the event that triggers the warning is triggered, the warning captures focus, clicks ok, the capture input focus, the focus event triggers a new event, now triggering two different warnings (the DOM is fully loaded, so a new event is added with another warning) both warnings capture the focus , click “OK”, click “OK”, “Input” will capture focus, fire a new event, now causing two different warnings, capture capture focus, click “OK”, next warning capture Focus on, press “OK”, input capture focus, trigger both events, click “OK”, the next warning will capture focus, click “OK”, “Take focus capture”, “Burn”, “Activate focus capture”, “OK” , “Confirm”, “Confirm”, “Confirm”, “Confirm”, “Paste”. focus capture warning, press “OK”, “capture input focus”, “fire”, “fire” and “event”, “capture focus input”, “fire”, “fire”, “fire”, “warning”, “ click "," next "," focus capture "," OK "," input focus capture ", both events are triggered, attention capture focus, click" OK ", the next warning will capture focus, click" OK ", enter focus capture, fire both events, input capture focus, both events are triggered, attention capture focus, click “OK”, the next warning captures focus, click “OK”, focus capture input, fires both events ...
Textual description of the endless FTW process! ....?: P
[/ edit]
In your previous examples using two automatic tricks, it seems that the latter will be executed, as in the example that I attached below. I also added a way to add a focus event to each input based on the class name ... Not sure if you are looking for this, but although this may bring some help.
JSFiddle onfocus event example