/    Sign up×
Community /Pin to ProfileBookmark

changing class through the dom

I want to change the class of a tag through the dom when it’s hovered. I do know how to program and stuff, but i confess to being quite new to javascript, dom, and the sort. heres my html:

[code=html]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html>
<head>
<title>experimental fun with ajax</title>

<meta http-equiv=”Content-Language” content=”EN-US” />
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />

<link rel=”stylesheet” href=”sheet.css” type=”text/css” />

<script src=”script.js” />

</head>
<body>
DYNAMIC LISTEY THING
<ul id=”dropdownlist”>
<li>Pirate Words
<ul id=”ul1″>
<li>yar</li>
<li>yar1</li>
<li>yar2</li>
<li>yar3</li>
</ul>
</li>
<li>Computer Words
<ul id=”ul2″>
<li>USB</li>
<li>PORT</li>
<li>BYTE</li>
<li>PROTOCOL</li>
</ul>
</li>
</ul>
</body>
</html>
[/code]

simple enough. i want the li’s directly under the ‘dropdownlist’ to change class onmouseover. here’s my script:

[code]window.onload = function onload(){

document.getElementById(‘dropdownlist’).getElementsByTagName(“li”).onmouseover = document.getElementById(‘dropdownlist’).getElementsByTagName(“li”).setAttribute(‘class’, ‘hovered’)

}
[/code]

it wasnt picking up the dropdownlist element before, now it’s telling me setattribute isnt a function. also, in it’s current state the page doesn’t show anything in ie. that might have something to do with the window.onload part, that seems to behave a bit differently in ie. HELP PLEASE!

for those who are interested, the purpose of this is to see if i can use javascript to change the class of something onmouseover, and if the item will then immediately take the traits of that class from the stylesheet on the fly. this would be a good alternative for the :hover with just css, as ie only allows for :hover on <a>.

If i can pull this off, i could write a script that duplicated the styles of all :hover parts of the stylesheet and made them separate classes, and had those elements changed to those classes onmouseover. perhaps even appending :hover to the classes of all elements onmouseover would be enough. that would mean that the addition of a simple script would allow for :hover css to be used on any element in ie. that would be pretty cool.

first priority is the immediate problem expressed above, but if you have any suggestions on this ultimate goal or if someone has already beat me to the same kind of script, please let me know. thanks in advance!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@themartySep 18.2006 — for those who are interested, the purpose of this is to see if i can use javascript to change the class of something onmouseover, and if the item will then immediately take the traits of that class from the stylesheet on the fly.[/quote]

yes this can be done.

this would be a good alternative for the :hover with just css, as ie only allows for :hover on <a>.[/quote]

If i'm not mistaken IE7 will finally allow :hover for other elements besides <a>

But for the other versions it's a good alternative.

first priority is the immediate problem expressed above,[/quote]

document.getElementById('someElement').className = 'new-name';

should do the trick

If you want a working example, this is a website in which i've done something similar:

http://www.huureenbedrijf.com/
Copy linkTweet thisAlerts:
@gameguy43authorSep 24.2006 — ok, thank you, that works. some of the code above it was throwing things off for some reason. but now i'm a bit confused. whenever i try to add a .mouseover thing as i do in my example code above, it just does the stuff after the equals when the page loads. the only way that i can truly add an onmouseover event is to run a setAttribute('onmouseover', "dosomething()"). is this the only solution? i suppose that it's still quite unobtrusive. forgive my pickiness but i would like to have no javascript even in the GENERATED page source. in other words, if someone could add the bit to marty's code that makes it change the class onmouseover of that tag, please show me! thank you.
Copy linkTweet thisAlerts:
@themartySep 25.2006 — put something like this in an external javascript file :
doMagic() = function()
{
document.getElementById('someEl').onmouseover=function()
{
this.className = "new-name";
}
document.getElementById('someEl').onmouseout=function()
{
this.className = "old-name";
}
}
window.onload=doMagic;


(of course you still need to alter it a bit)
×

Success!

Help @gameguy43 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.25,
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,
)...