472,353 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

onmouseover event function for dom created div

I have written some dom code to create a list of divs, each with it's
own id. I want to set the onmouseover and onmouseout events to
highlight the div when the mouse is over it. However I cannot use the
method below because oDiv.id is always set to the last div I create -
so the last div is highlighted regardless of which div I am onmouseover
This must be a common issue, how do I go about fixing it?

I can have a separate function which takes event.srcElement and tracks
back through the parent elments until it finds a div with an id
starting with "entry_" but I was hoping for an easier option.

Is this something to do with closures?

Here's a much simplified example :

for( nIndex=0; nIndex<aEntries.length; nIndex++)
{
oEntry = aEntries[nIndex];

oDiv = document.createElement( "div");
oDiv.id = "entry_" + oEntry.uniquename;
oDiv.onmouseover = function() {document.getElementById(
oDiv.id).className = "hover";};
oDiv.onmouseout = function() {document.getElementById(
oDiv.id).className = "";};

oBody.appendChild( oDiv)
}

Nov 7 '06 #1
3 3483

oo******@yahoo.co.uk wrote:
I have written some dom code to create a list of divs, each with it's
own id. I want to set the onmouseover and onmouseout events to
highlight the div when the mouse is over it. However I cannot use the
method below because oDiv.id is always set to the last div I create -
so the last div is highlighted regardless of which div I am onmouseover
This must be a common issue, how do I go about fixing it?
You have discovered closures. Each mouse event gets a reference to the
same id variable, so they all get the same value.

I can have a separate function which takes event.srcElement and tracks
back through the parent elments until it finds a div with an id
starting with "entry_" but I was hoping for an easier option.
There is no need for that, or to use getElementById.

Is this something to do with closures?
Yes.

Here's a much simplified example :

for( nIndex=0; nIndex<aEntries.length; nIndex++)
{
oEntry = aEntries[nIndex];

oDiv = document.createElement( "div");
oDiv.id = "entry_" + oEntry.uniquename;
oDiv.onmouseover = function() {document.getElementById(
oDiv.id).className = "hover";};
oDiv.onmouseover = function() {this.className = "hover";};

oDiv.onmouseout = function() {document.getElementById(
oDiv.id).className = "";};
oDiv.onmouseout = function() {this.className = "";};

oBody.appendChild( oDiv)
}
--
Rob

Nov 7 '06 #2

Thanks a lot Rob, I knew it would need a problem coming up in one of my
own projects before I understood closures!

The solution is even easier than I hoped for
Is this something to do with closures?

Yes.

Here's a much simplified example :
oDiv.onmouseover = function() {document.getElementById(
oDiv.id).className = "hover";};

oDiv.onmouseover = function() {this.className = "hover";};

oDiv.onmouseout = function() {document.getElementById(
oDiv.id).className = "";};

oDiv.onmouseout = function() {this.className = "";};

Rob
Nov 7 '06 #3
ASM
oo******@yahoo.co.uk a écrit :
I have written some dom code to create a list of divs, each with it's
own id. I want to set the onmouseover and onmouseout events to
highlight the div when the mouse is over it.
Supposing id of all these new divs begin with 'new_'

function setHover(idStart) {
var T = document.getElementsByTagName('div');
for(var i=0; i<T.length; i++)
if(T[i].id.indexOf(idStart)>=0) {
T[i].onmouseover = function() { roll(this) }
T[i].onmouseout = function() { roll(this) }
T[i].style.cursor = 'pointer';
}
}
function roll(what) {
what = what.style;
what.backgoundColor = what.backgoundColor==''? '#ffc' : '';
}
onload = function() { setHover('new_'); }
You can in function stHover() also have :

var T = document.getElementsByTagName('*');

and it'll work with each tag of the file.
However I cannot use the
method below because oDiv.id is always set to the last div I create -
so the last div is highlighted regardless of which div I am onmouseover
Je ne comprends pas.
Why the new div is set *to* the previous one ?
This must be a common issue, how do I go about fixing it?
I can have a separate function which takes event.srcElement and tracks
back through the parent elments until it finds a div with an id
starting with "entry_"
Ha! my example above is of no use !
but I was hoping for an easier option.

Is this something to do with closures?

Here's a much simplified example :

for( nIndex=0; nIndex<aEntries.length; nIndex++)
{
oEntry = aEntries[nIndex];
var oEntry = aEntries[nIndex];
>
oDiv = document.createElement( "div");
var oDiv = document.createElement( "div");
oDiv.id = "entry_" + oEntry.uniquename;
oDiv.onmouseover = function() { roll(this); }
oDiv.onmouseout = function() { roll(this); }
oDiv.onmouseover = function() {document.getElementById(
oDiv.id).className = "hover";};
oDiv.onmouseover = function() { this.className = 'hover';};
oDiv.onmouseout = function() {document.getElementById(
oDiv.id).className = "";};
oDiv.onmouseout = function() { this.className = '';};

oDiv.style.cursor = 'pointer'; // if not in styles sheet
oBody.appendChild( oDiv)
document.body.appendChild(oDiv);
}
Nov 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Epetruk | last post by:
Hi all, I want a page where the contents of a table cell are replaced with an image when the mouse moves over the cell, and the text is restored...
7
by: windandwaves | last post by:
Hi Gurus I am trying to make this rather complicated maps with an a huge number of mouseovers and the like. I want to set up a function that...
5
by: Ryan Moore | last post by:
I am trying to modify the onMouseOver attribute of a <td> cell created by a DataList... according to ...
3
by: drjackk | last post by:
Hello, I'm trying to change the onmouseover event dynamically. This sets-up the initial onmouseover event: <a href="home.html"> <img...
2
by: Justin Rowe | last post by:
I'm attempting to design a site with alot of dynamic content and intractability, however I've hit a snag when it comes to the function of the...
1
by: den2005 | last post by:
Hi everybody, I am confused and still looking why this codes is not working. Can anyone notice or know why this code is not working? Thanks in...
5
by: Sabbaath | last post by:
First off, I've pretty much convinced myself that the issue is that there are onmouseover events inside the div that contains an onmouseover event on...
3
by: linuskamb | last post by:
It seems, at least as far as my tests show, that Safari does not fire onmouseover for select options. with the code below, I can get all the...
5
by: jkershner | last post by:
I have a js script inside php code that is connected to mysql, the php code is listing the results from the query then, the onmouseover function is...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.