473,587 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.srcElemen t 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.create Element( "div");
oDiv.id = "entry_" + oEntry.uniquena me;
oDiv.onmouseove r = function() {document.getEl ementById(
oDiv.id).classN ame = "hover";};
oDiv.onmouseout = function() {document.getEl ementById(
oDiv.id).classN ame = "";};

oBody.appendChi ld( oDiv)
}

Nov 7 '06 #1
3 3612

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.srcElemen t 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.create Element( "div");
oDiv.id = "entry_" + oEntry.uniquena me;
oDiv.onmouseove r = function() {document.getEl ementById(
oDiv.id).classN ame = "hover";};
oDiv.onmouseove r = function() {this.className = "hover";};

oDiv.onmouseout = function() {document.getEl ementById(
oDiv.id).classN ame = "";};
oDiv.onmouseout = function() {this.className = "";};

oBody.appendChi ld( 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.onmouseove r = function() {document.getEl ementById(
oDiv.id).classN ame = "hover";};

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

oDiv.onmouseout = function() {document.getEl ementById(
oDiv.id).classN ame = "";};

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(idStar t) {
var T = document.getEle mentsByTagName( 'div');
for(var i=0; i<T.length; i++)
if(T[i].id.indexOf(idS tart)>=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.backgoundC olor = what.backgoundC olor==''? '#ffc' : '';
}
onload = function() { setHover('new_' ); }
You can in function stHover() also have :

var T = document.getEle mentsByTagName( '*');

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.srcElemen t 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.create Element( "div");
var oDiv = document.create Element( "div");
oDiv.id = "entry_" + oEntry.uniquena me;
oDiv.onmouseove r = function() { roll(this); }
oDiv.onmouseout = function() { roll(this); }
oDiv.onmouseove r = function() {document.getEl ementById(
oDiv.id).classN ame = "hover";};
oDiv.onmouseove r = function() { this.className = 'hover';};
oDiv.onmouseout = function() {document.getEl ementById(
oDiv.id).classN ame = "";};
oDiv.onmouseout = function() { this.className = '';};

oDiv.style.curs or = 'pointer'; // if not in styles sheet
oBody.appendChi ld( oDiv)
document.body.a ppendChild(oDiv );
}
Nov 7 '06 #4

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

Similar topics

12
12259
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 when the mouse moves out. Here's the html for the page: <HTML> <HEAD> <style type="text/css">
7
3331
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 OnMouseDown swaps the OnMouseOver and OnMouseOut for the same element. E.g. <A HREF="#" OnMouseOver="A(); return true" OnMouseOut"B(); return true;"
5
4684
by: Ryan Moore | last post by:
I am trying to modify the onMouseOver attribute of a <td> cell created by a DataList... according to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclasstopic.asp "You can control the appearance of the DataGrid control by programmatically adding attributes to the <td> and...
3
3939
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 border="0" id="img22" src="images/home1.jpg" height="15" width="85" alt="Home" onmouseover="FP_swapImg(1,0,/*id*/'img22',/*url*/'images/home2.jpg')"</a>
2
3346
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 onMouseOver and onMouseOut events. Using a bit of code from QuirksMode to grab the location of the mouse, I've built a tooltip function to show a tooltip to the user depicting the target of a link, the...
1
18845
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 advance. Code working: <form id="form1" runat="server"> <div> &nbsp;</div> <div>
5
3131
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 it... I wrote the message below first. This was added as I sent. Original: My function is firing with every move of the mouse in the onmouseOver area. I've assigned the onmouseOver event...
3
4458
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 messages in Firefox, but none in Safari. If I attach a handler for the onmouseover to the select element (the part currently commented out) I can get the first mouseover of the select element, but not any...
5
4621
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 supposed to show the description of the title for that specific title, however, the mouseover is displaying all the descriptions for every title. Also, will you let me know if I have code in here that...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.