Skip to content

Instantly share code, notes, and snippets.

@Tatsh
Last active July 13, 2020 18:22
Show Gist options
  • Select an option

  • Save Tatsh/f7f4dadcd5fbecfa444a to your computer and use it in GitHub Desktop.

Select an option

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'
// 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()
});
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);
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