I'm working in a little app, mostly using jquery, everything is working fine but I'd like to rewrite every jquery part with plain javascript to fully understand what I'm doing.
I've read this previus post, and everything makes perfect sense but I can't get any code to run in my browser.
How to select all <a> tag and register onclick event?
This is my code:
<html>
<head>
<script type="text/javascript">
var a = document.getElementsByTagName('a');
var f = function () {alert('ok');}
for (var i = a.length - 1; i >= 0; i--) {
a[i].onClick = f;
}
</script>
</head>
<body>
<a href="#">test1</a>
<a href="#">test2</a>
</body>
</html>
In jquery I'd simply use this to find all anchors and run my function when clicked:
$('a').on('click',function (){
alert('ok');
});
Any thoughts? thanks in advance, Gustavo