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

createElement('a') and mouseover

P: n/a
Kim
Hi
I'm trying to do something like this.

for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = function() { return get(i);}
...
...
}

But it won't work, because all the get(i) is get(4)...
I know why this happens, but I don't know how to solve it...

anybody have an idea?
Sep 28 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Kim wrote on 28 sep 2005 in comp.lang.javascript:
Hi
I'm trying to do something like this.

for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = function() { return get(i);}
...
...
}

But it won't work, because all the get(i) is get(4)...
I know why this happens, but I don't know how to solve it...

anybody have an idea?


Elementary my dear Kim.
var theData = document.createElement('a');
// take this non changing line outside the for-loop

for(i=0; i<5; i++){
theData.onmouseover = function() { return get(i);}
}

theData.onmouseover will be reassigned to eachtine another function,
and only the last one is current after the for-loop.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 28 '05 #2

P: n/a


Kim wrote:

for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = function() { return get(i);}
...
...
}

But it won't work, because all the get(i) is get(4)...


You want
theData.onmouseover = new Function('evt', 'return get(' + i + ');');
then.
Or you would need to somehow attach the i value to the a element object e.g.
theData.someProperty = i;
theData.onmouseover = function (evt) { return get(this.myProperty); };

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 28 '05 #3

P: n/a
>> for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = function() { return get(i);}
...
}


Elementary my dear Kim.
var theData = document.createElement('a');
// take this non changing line outside the for-loop
for(i=0; i<5; i++){
theData.onmouseover = function() { return get(i);}
}
Evertjan.


I'm pretty sure the OP wants to generate 5 links. Your suggestion only
generates 1 link and then assigns 5 functions to it, each overwriting
the last.

Would this work?:
for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = new Function("return " + i + ";");
....
}

Sep 28 '05 #4

P: n/a
Jambalaya wrote on 28 sep 2005 in comp.lang.javascript:
I'm pretty sure the OP wants to generate 5 links. Your suggestion only
generates 1 link and then assigns 5 functions to it, each overwriting
the last.

Would this work?:
for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = new Function("return " + i + ";");
...
}


Well yes, but returning a value to a <a> mouseover is not doing anything.

Try this [ie6 tested]:

==========
<body>

<script type='text/javascript'>

for(i=0; i<5; i++){
myLink = document.createElement("a");
myLink.href = "http://www.cnn.com/";
myLink.innerHTML='CNN: '+i+'<br>'
myLink.onmouseover = new Function("alert('number: "+i+"');");
document.body.appendChild(myLink);
}

</script>
==========

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 28 '05 #5

P: n/a
ASM
Kim a écrit :
Hi
I'm trying to do something like this.

for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = function() { return get(i);}
...
...
}

But it won't work, because all the get(i) is get(4)...
I know why this happens, but I don't know how to solve it...


theData.onmouseover = 'return get('+i+');';
--
Stephane Moriaux et son [moins] vieux Mac
Sep 28 '05 #6

P: n/a
Kim
Thanks that worked...
...for IE6, but not firefox
so..next question... can it be done in firefox?
"Evertjan." <ex**************@interxnl.net> skrev i en meddelelse
news:Xn********************@194.109.133.242...
Jambalaya wrote on 28 sep 2005 in comp.lang.javascript:
I'm pretty sure the OP wants to generate 5 links. Your suggestion only
generates 1 link and then assigns 5 functions to it, each overwriting
the last.

Would this work?:
for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = new Function("return " + i + ";");
...
}


Well yes, but returning a value to a <a> mouseover is not doing anything.

Try this [ie6 tested]:

==========
<body>

<script type='text/javascript'>

for(i=0; i<5; i++){
myLink = document.createElement("a");
myLink.href = "http://www.cnn.com/";
myLink.innerHTML='CNN: '+i+'<br>'
myLink.onmouseover = new Function("alert('number: "+i+"');");
document.body.appendChild(myLink);
}

</script>
==========

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 28 '05 #7

P: n/a
Kim wrote on 29 sep 2005 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> skrev i en meddelelse
news:Xn********************@194.109.133.242...
Try this [ie6 tested]:

==========
<body>

<script type='text/javascript'>

for(i=0; i<5; i++){
myLink = document.createElement("a");
myLink.href = "http://www.cnn.com/";
myLink.innerHTML='CNN: '+i+'<br>'
myLink.onmouseover = new Function("alert('number: "+i+"');");
document.body.appendChild(myLink);
}

</script>
==========

[please do not toppost on usenet, Kim]
Thanks that worked...
..for IE6, but not firefox
so..next question... can it be done in firefox?


You answered it yourself, it seems, I did not test that.

I am not "into" firefox,
perhaps the innerHTML needs to be exchanged for a DOM method?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 29 '05 #8

P: n/a
"Kim" <No@thanks.com> wrote in message
news:43*********************@dread11.news.tele.dk. ..
Thanks that worked...
..for IE6, but not firefox
so..next question... can it be done in firefox?
"Evertjan." <ex**************@interxnl.net> skrev i en meddelelse
news:Xn********************@194.109.133.242...
Jambalaya wrote on 28 sep 2005 in comp.lang.javascript:
I'm pretty sure the OP wants to generate 5 links. Your suggestion only
generates 1 link and then assigns 5 functions to it, each overwriting
the last.

Would this work?:
for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = new Function("return " + i + ";");
...
}


Well yes, but returning a value to a <a> mouseover is not doing anything.
Try this [ie6 tested]:

==========
<body>

<script type='text/javascript'>

for(i=0; i<5; i++){
myLink = document.createElement("a");
myLink.href = "http://www.cnn.com/";
myLink.innerHTML='CNN: '+i+'<br>'
myLink.onmouseover = new Function("alert('number: "+i+"');");
document.body.appendChild(myLink);
}

</script>


Try:

function createHREF(text,href)
{
var a = document.createElement("A")
document.body.appendChild(a)
var t = document.createTextNode()
t.data = text
a.href = href
a.onmouseover = gotoHREF
a.appendChild(t)
a.appendChild(document.createElement("BR"))
return a
}

function gotoHREF()
{
window.open(this.href) // Opens a new window.
//window.location = this.href
//Above opens in same window.
}

var hrefs = [ ["Yahoo","http://www.yahoo.co.uk"],
["Hotmail","http://www.hotmail.co.uk"] ]

for( var i = 0 ; i < hrefs.length ; i++ )
createHREF(hrefs[i][0],hrefs[i][1])

*************************************

I'm assuming that you're trying to get a mouseover event to change the URL
of the page. You didn't say.

--
Wayne
"Aka Dobbie the House Elf."
Sep 29 '05 #9

P: n/a
["Followup-To:" header set to alt.comp.lang.javascript.]
On 2005-09-28, Kim <No@thanks.com> wrote:
Hi I'm trying to do something like this.

for(i=0; i<5; i++){
var theData = document.createElement('a');
theData.onmouseover = function() { return get(i);}
...
...
}

But it won't work, because all the get(i) is get(4)...
I know why this happens, but I don't know how to solve it...


what do you want it to do?
Bye.
Jasen
Oct 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.