473,418 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

createElement('a') and mouseover

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
9 7939
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


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
>> 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
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
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
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
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
"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
["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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
2
by: kie | last post by:
hello, when i create elements and want to assign events to them, i have realised that if the function assigned to that element has no parameters, then the parent node values can be attained. ...
14
by: J. Makela | last post by:
Hallo. This should be a pretty entertaining question for you *real* javascript writers.. I, being the lowly photoshop guy at an ad agency made the mistake of actually saying "HTML" in a...
2
by: Alex | last post by:
On my page I have a lot string like this: <span onmouseover="callMe(this)" onmouseout="callMe(null)" >abc1</span> <span onmouseover="callMe(this)" onmouseout="callMe(null)" >abc2</span> <span...
17
by: mouseit101 | last post by:
Hello, I'm trying to make a navigation bar and need to assign mouseover, mouseclick etc. events to a img tag dynamically, here's what im using: function getP_Element(p_imgsrc,p_id) {var tuf =...
3
by: Annette Acquaire | last post by:
I have and image map with a dozen hotspot links on it that I'm trying to get to open a new image over existing one on mouseover of each COORD. The only thing I was able to do was swap image on...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
23
by: Schannah | last post by:
I'm trying to create a design which mimics the Radiohead website in the action on this page, but the problem is that they use PHP for the effect and I have no idea about PHP. I'm very amateur: fairly...
1
by: dave345 | last post by:
This javascript issue is in an app using C# / .Net 2.0 running on XP. First post, please mention if I mess up any conventions of this forum. I’ve got a mouseover event that only works properly...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.