I am new 2 javascript i have created function that adds onclick event & calls a user function to all my links in a page but it not working properly help me to solve this
<script type="text/javascript">
window.onload=function() {
var links = document.getElementsByTagName('a');
for(var i = 0, i<links.length; i++) {
links[i].onclick = function () {
var string = links[i].href; //href value
var str = string;
var spl = string.split("/");
switch(spl[2])
{
case 'www.google.com':
var str1 = "http://yahoo.com";
links[i].target="_blank";
links[i].href= str1;
break;
default:
links[i].href= string;
}
}
}
}
</script>
<a href="http://www.google.com/" target="-blank">www.google.com</a></br>
elements[i]in the onclick function, that will always be the last element, since the for loop will be finished when the onclick function is called. is that your error?