Last active
July 13, 2020 18:22
-
-
Save Tatsh/f7f4dadcd5fbecfa444a to your computer and use it in GitHub Desktop.
Acceptable answers for 'Make every link on the page alert with 'Hello' and not navigate'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Only partially acceptable, raw JS/DOM knowledge is preferred | |
| $('a').on('click', function () { | |
| alert('Hello'); | |
| return false; // or take in event argument and use event.preventDefault() | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var list = document.getElementsByTagName('a'); | |
| (function namedFunction(listOfLinks, i) { | |
| var l = listOfLinks[i]; | |
| if (l === void 0) { // can also use typeof l === 'undefined', but no comparison to undefined | |
| return; | |
| } | |
| l.addEventListener( | |
| 'click', | |
| (function () { | |
| return function(e) { | |
| alert('Hello'); | |
| e.preventDefault(); | |
| }; | |
| })(), | |
| false); | |
| namedFunction(listOfLinks, i + 1); | |
| })(list, 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var links = document.getElementsByTagName('a'); | |
| for (var i = 0, j = links.length; i < j; i++) { | |
| links[i].addEventListener('click', (function() { | |
| return function(e) { | |
| alert('Hello'); | |
| e.preventDefault(); | |
| }; | |
| })(), false); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment