By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,571 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,571 IT Pros & Developers. It's quick & easy.

special "for" not working and passing arguments to events

P: n/a
Hello group.

In the following testcase I attach to the final of the message, I have
two questions:

1) According to comment # 1, how can I pass an argument to the
function that way?

2) According to comment # 2, why this special structure for the "for"
statement is not working?


<script type="text/javascript" language="JavaScript">
function Alert(number) {
window.alert("A element clicked!(number:" + number + ")");

return false;

function ApplyAlerts() {
var oAElement = window.document.getElementById('alert1');
oAElement.onclick = Alert; //how can I pass an argument? [comment

var aElements = window.document.getElementsByTagName('a');

for (var i = aElts.length; i--; ) { var oElement = aElts[i]; //this
//for (oElement in aElts){ //Why this doesnt work? [comment #2]

if (
if ( == 'alert2')
oElement.onclick = Alert;

window.onload = ApplyAlerts;
<body id="body">
<div id="test">
<a id="alert1" name="alert1" href=""
target="_blank">Link1</a><br />
<a id="alert2" name="alert2" href=""
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
knocte wrote:
oAElement.onclick = Alert; //how can I pass an argument? [comment
Use a continuation:

oAElement.onclick = function() { Alert(42); };

[...] //for (oElement in aElts){ //Why this doesnt work? [comment #2]

You're not using IE5, are you? It has lousy Javascript support, including
not supporting the 'in' token. (It also barfs at // comments. Helpful.)

+- David Given --McQ-+
| | Wholesale theft is the sincerest form of flattery.
| ( |
+- --+

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.