/    Sign up×
Community /Pin to ProfileBookmark

Event Listener Question

I am trying to write some code that will add a couple of event listeners to a number of certain list elements. I am trying to add a mouseover and mouseout css style effect. I know I can do this with CSS, but I’m having problems with IE8 and some other things on my page for some reason (definitely a bug).

How can I reference the element dynamically? this is not working for me nor is getLI[o]. I’m getting alerts just fine, but I need to access each elements style. Thoughts?

var getLI = document.getElementsByTagName(“li”);
for (var o=0;o < getLI.length;o++) {
if (getLI[o].parentNode.id == “subRooms”) {
if (document.body.addEventListener) {
getLI[o].addEventListener(‘onmouseover’, function() { alert(“over”); }, true);
getLI[o].addEventListener(‘onmouseout’, function() { alert(“out”); }, true);
} else if (document.body.attachEvent) {
getLI[o].attachEvent(‘onmouseover’, function() { this.style.color = ‘red’; }, true);
getLI[o].attachEvent(‘onmouseout’, function() { alert(“out”); }, true);
} else {
getLI[o].onmouseover = function() { alert(“asdf”); };
getLI[o].onmouseout = function() { alert(“fdsa”); };
}
}
}

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @rorktor1 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 5.9,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...