/    Sign up×
Community /Pin to ProfileBookmark

Adding onmouseover eventhandler to element

Ok, this is quite a complicated issue that I am having here, at least I think. I have a javascript function called setMouseovers, that iterates through a loop, and adds mouseover event handlers to items on the page, based on id. Initially I was using element.setAttribute(‘onmouseover’, ‘do-something’) which worked great in ff, but does nothing in ie – see this link: [URL=”http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html”]http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html[/URL]

Anyways, I was forced to use the .onmouseover property to dynamically set onmouseover methods. Here is the resulting code:

[code=html]
<html>
<head>
<style type=”text/css”>
div
{
width: 100px;
border: 1px solid #000;
}
</style>
<script type=”text/javascript”>
function setMouseOvers()
{
for (var i=1;i<9;i++){
var div = document.getElementById(‘div’ + i);
div.onmouseover = function(){alert(i);};
}
}
window.onload = setMouseOvers;
</script>
</head>
<body>
<div id=”div1″> div #1 </div>
<div id=”div2″> div #2 </div>
<div id=”div3″> div #3 </div>
<div id=”div4″> div #4 </div>
<div id=”div5″> div #5 </div>
<div id=”div6″> div #6 </div>
<div id=”div7″> div #7 </div>
<div id=”div8″> div #8 </div>
</body>
</html>
[/code]

The problem is that, the alert is alerting the user with the value of i at the time the div is moused over, as opposed to the time that the onmouseover function is being created, and I would like the opposite. If you don’t understand what I mean, copy the above html into notepad and play around. Any help is appreciated here. Thanks.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CharlesFarleyMar 26.2008 — Hi

You need to alert from the element id reference which remains permanent rather than the variable i which changes. change your code to this:

div.onmouseover = function(){alert(this.id.slice(3));}

Gord
Copy linkTweet thisAlerts:
@KorMar 27.2008 — give the objects a custom property:
<i>
</i>function setMouseOvers()
{
var div;
for (var i=1;i&lt;9;i++){
div=document.getElementById('div' + i);
div.ind=i;
div.onmouseover=function(){alert(this.ind)}
}
}
Copy linkTweet thisAlerts:
@UltimaterMar 30.2008 — Solution #1:
<i>
</i>function setMouseOvers(){
for (var i=1;i&lt;9;i++){
var div = document.getElementById('div' + i);
div.onmouseover = [color=blue]new Function("alert("+i+")");[/color]
}
}


Solution #2:
<i>
</i>function setMouseOvers(){
for (var i=1;i&lt;9;i++){
var div = document.getElementById('div' + i);
[color=blue](function(i){[/color]
div.onmouseover = function(){alert(i);};
[color=blue]})(i);[/color]
}
}


You encountered the issue in the first place since the inner function has access to setMouseOvers' local variables thus any changes to a variable local to the outer function will be reflected upon the inner function which references it. Thus you are creating 8 separate functions each referencing [b]i[/b] defined in setMouseOvers and any changes to [b]i[/b] will impact the other functions that reference it. You might want to google scopes or closures for more info.
×

Success!

Help @mateobus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...