Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kuznetsovandrey76/39a1fba45c212c32415d000743211ab9 to your computer and use it in GitHub Desktop.
Save kuznetsovandrey76/39a1fba45c212c32415d000743211ab9 to your computer and use it in GitHub Desktop.
var div = document.querySelectorAll('div'),
result;
for (var i = 0; i < div.length; i++) {
result = div[i];
result.addEventListener('click', function() {
alert(this.innerHTML);
});
}
@NoxFly
Copy link

NoxFly commented Jun 14, 2019

Nice, it's thanks you I got the answer. (2 years later ^^)
You can add another thing to your code now that there is EMACs 6 on JS:

document.selectorAll('div').forEach(div => {
    div.addEventListener('click', () => {
        alert(div.innerHTML);
    });
});

Instead of doing the for loop, use the forEach to select each 'div' element of the selectorAll, then apply the listener.

@vassiliy278
Copy link

Thank You. Useful Tip!

@mahanmashoof
Copy link

Thanks!

@johnson-maria
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment