Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Attach onclick event to links programmatically

Reply
Thread Tools

Attach onclick event to links programmatically

 
 
Miroslav Stampar [MCSD.NET / Security+]
Guest
Posts: n/a
 
      06-02-2007
what am i doing wrong. i want to attach onclick event to links. i am
using code below but it doesn't work as it should. plz help.

<html>
<head>
<title>getElementById example</title>
</head>

<body>
<div id="here">
<td>
<a href ="http://www.google.com">Google</a>
<a href ="http://www.yahoo.com">Yahoo</a>
</td>
</div>

<script>
var elementDiv = document.getElementById('here');
var elementsA = elementDiv.getElementsByTagName('a');

for(var i=0;i<elementsA.length;i++)
{
elementsA[i].onclick="alert('BLA')";
alert(elementsA[i].href);
}
</script>

</body>
</html>


p.s.

I want to do this:

<html>
<body>
<a href="http://www.google.com" onclick="alert('BLA')">Google</a>
</body>
</html>

THANKS IN ADVANCE

 
Reply With Quote
 
 
 
 
Miroslav Stampar [MCSD.NET / Security+]
Guest
Posts: n/a
 
      06-02-2007
Sorry for bothering. I've found the way:

<html>
<head>
<title>getElementById example</title>
</head>

<body>
<div id="here">
<td>
<a href ="http://www.google.com">Google</a>
<a href ="http://www.yahoo.com">Yahoo</a>
</td>
</div>

<script>

function fja()
{
alert("BLA");
}

var elementDiv = document.getElementById('here');
var elementsA = elementDiv.getElementsByTagName('a');

for(var i=0;i<elementsA.length;i++)
{
elementsA[i].onclick=fja;
alert(elementsA[i].href);
}

</script>

</body>
</html>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any shortcut for "Debug > Attach > Attach to aspnet_wp.exe"? Warren Tang ASP .Net 1 09-17-2008 03:46 PM
attach agent to applet running in firefox (attach api) craiget@gmail.com Java 0 06-14-2007 12:28 AM
Attach event/function to links within span tag johkar Javascript 22 01-23-2006 07:19 PM
UI: assignment of links using onclick event handler Dark Magician Javascript 1 04-08-2005 09:44 PM
Writing hyperlinks that attach to desktop icons or links Bill ASP General 1 10-22-2003 07:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57