HighDots Forums  

createElement('a') and mouseover

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss createElement('a') and mouseover in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Kim
 
Posts: n/a

Default createElement('a') and mouseover - 09-28-2005 , 12:20 PM






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?



Reply With Quote
  #2  
Old   
Evertjan.
 
Posts: n/a

Default Re: createElement('a') and mouseover - 09-28-2005 , 12:28 PM






Kim wrote on 28 sep 2005 in comp.lang.javascript:

Quote:
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)



Reply With Quote
  #3  
Old   
Jambalaya
 
Posts: n/a

Default Re: createElement('a') and mouseover - 09-28-2005 , 01:54 PM



Quote:
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 + ";");
....
}



Reply With Quote
  #4  
Old   
Evertjan.
 
Posts: n/a

Default Re: createElement('a') and mouseover - 09-28-2005 , 03:02 PM



Jambalaya wrote on 28 sep 2005 in comp.lang.javascript:

Quote:
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)



Reply With Quote
  #5  
Old   
ASM
 
Posts: n/a

Default Re: createElement('a') and mouseover - 09-28-2005 , 03:47 PM



Kim a écrit :
Quote:
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


Reply With Quote
  #6  
Old   
Kim
 
Posts: n/a

Default Re: createElement('a') and mouseover - 09-28-2005 , 06:15 PM



Thanks that worked...
...for IE6, but not firefox
so..next question... can it be done in firefox?


"Evertjan." <exjxw.hannivoort (AT) interxnl (DOT) net> skrev i en meddelelse
news:Xns96DFE04B047B7eejj99 (AT) 194 (DOT) 109.133.242...
Quote:
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)




Reply With Quote
  #7  
Old   
Evertjan.
 
Posts: n/a

Default Re: createElement('a') and mouseover - 09-29-2005 , 02:56 AM



Kim wrote on 29 sep 2005 in comp.lang.javascript:

Quote:
"Evertjan." <exjxw.hannivoort (AT) interxnl (DOT) net> skrev i en meddelelse
news:Xns96DFE04B047B7eejj99 (AT) 194 (DOT) 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]

Quote:
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)



Reply With Quote
  #8  
Old   
Wayne Dobson
 
Posts: n/a

Default Re: createElement('a') and mouseover - 09-29-2005 , 05:11 AM



"Kim" <No (AT) thanks (DOT) com> wrote

Quote:
Thanks that worked...
..for IE6, but not firefox
so..next question... can it be done in firefox?


"Evertjan." <exjxw.hannivoort (AT) interxnl (DOT) net> skrev i en meddelelse
news:Xns96DFE04B047B7eejj99 (AT) 194 (DOT) 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."




Reply With Quote
  #9  
Old   
Jasen Betts
 
Posts: n/a

Default Re: createElement('a') and mouseover - 10-15-2005 , 04:50 PM



["Followup-To:" header set to alt.comp.lang.javascript.]
On 2005-09-28, Kim <No (AT) thanks (DOT) com> wrote:
Quote:
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


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.