/    Sign up×
Community /Pin to ProfileBookmark

Absolute beginner – style not changing

I have written this, amongst other stuff:

[code=html]<!DOCTYPE HTML>

<html>
<head>
<title>HTML / CSS</title>
<script type=”text/javascript” >
function hide()
{
this.style.color = ‘red’;
}
</script>
</head>

<body>
<div>
<ul class=”top” style=”position: relative; display: block; “>
<li>
<a href=”#”>HOME</a>
</li>
<li>
<a href=”#” onmouseover=”hide()”>SUBJECTS</a>
</li>
</ul>
</div>
</body>
</html>
[/code]

I am trying to get the color of the list item ‘SUBJECTS’ to turn red on mouse over. I think my syntax must be wrong but I don’t know how so. What can I do to fix it?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienJan 29.2011 — I have written this, amongst other stuff:
[code=html]<!DOCTYPE HTML>

<html>
<head>
<title>HTML / CSS</title>
<script type="text/javascript" >
function hide()
{
this.style.color = 'red';
}
</script>
</head>

<body>
<div>
<ul class="top" style="position: relative; display: block; ">
<li>
<a href="#">HOME</a>
</li>
<li>
<a href="#" onmouseover="hide()">SUBJECTS</a>
</li>
</ul>
</div>
</body>
</html>
[/code]

I am trying to get the color of the list item 'SUBJECTS' to turn red on mouse over. I think my syntax must be wrong but I don't know how so. What can I do to fix it?[/QUOTE]


When using "this" inside your function, it actually references the window object, not the DOM node that was just moused over. Instead, you want this:

[CODE]<a href="#" onmouseover="hide([B]this[/B])">SUBJECTS</a[/CODE]

And JavaScript:
[CODE]function hide([B]node[/B]) {
[B]node[/B].style.color = "red";
}[/CODE]
Copy linkTweet thisAlerts:
@sAuhsojauthorJan 29.2011 — ah right! thanks a lot. not exactly intuitive but I will pick it up
Copy linkTweet thisAlerts:
@sAuhsojauthorJan 29.2011 — I'm now using jQuery and I'm trying to write this piece of HTML but document.write('') will overwrite the entire page. I've got this:
[code=html]
<script type="text/javascript" >
var foot="<div class="footer"><marquee BEHAVIOR="SLIDE" direction="up" scrollamount="9" height="40"><div><ul class="footertools" style="position: relative; display: block; "><li><a href="http://www.facebook.com/profile.php?id=100001427582191" onclick="javascript:fb_alert()" target="_blank" class="bottom" style="border:0px; -moz-border-radius-topright: 9px 9px; border-top-right-radius: 9px 9px; ">fb</a></li><li><a href="URL OF PAGE" class="bottom">privacy</a></li></ul></div></marquee></div>";
function footer()
{
document.write(foot);
}
$(document).ready(function(){
footer()
});

</script>
[/code]
Copy linkTweet thisAlerts:
@toicontienFeb 01.2011 — After a certain point you cannot call document.write() because it literally writes over the page. You'll need to append that HTML to the end of the page:

[CODE]$(document).ready(function() {
$("body").append("<div class="footer"><marquee BEHAVIOR="SLIDE" direction="up" scrollamount="9" height="40"><div><ul class="footertools" style="position: relative; display: block; "><li><a href="http://www.facebook.com/profile.php?id=100001427582191" onclick="javascript:fb_alert()" target="_blank" class="bottom" style="border:0px; -moz-border-radius-topright: 9px 9px; border-top-right-radius: 9px 9px; ">fb</a></li><li><a href="URL OF PAGE" class="bottom">privacy</a></li></ul></div></marquee></div>");
});[/CODE]
×

Success!

Help @sAuhsoj 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.19,
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,
)...