/    Sign up×
Community /Pin to ProfileBookmark

Simple page using onMouseover and OnMouseout

I created a simple page to get a good understanding of using onMouseover and onMouseout.

When hovering the mouse over any of the images and then moving them away, it does work as it should (the background color of the buttons change color and then change back). However, when the page [U]initially[/U] loads, the background color of the buttons is not blue eventhough I do have it defined in my code. I viewed the page both in Firefox and IE. It’s not until you hover the mouse over one of the images and then move it away that the background color changes to blue. Why is not blue when the page initially loads?

[URL=”http://goken68.brinkster.net/RolloverEffects.html”]http://goken68.brinkster.net/RolloverEffects.html[/URL]

[CODE]

<html>
<head>
<script type=”text/javascript” language=”javascript”>

contactImage = new Image();
contactImage.src = ‘americanpride.jpg’;

contactImage2 = new Image();
contactImage2.src = ‘baldeagle.jpg’;

contactImage3 = new Image();
contactImage3.src = ‘ProudToBeAmerican.jpg’;

function loadImage()
{
document.getElementById(‘imgContact’).src = contactImage.src;
document.getElementById(‘imgContact2’).src = contactImage2.src;
document.getElementById(‘imgContact3’).src = contactImage3.src;
}

function changeIt()
{
first = document.getElementById(‘Button1’);
first.style.backgroundColor=”green”;
}

function changeBack()
{
first = document.getElementById(‘Button1’);
first.style.backgroundColor=”blue”;
}

function changeIt2()
{
second = document.getElementById(‘Button2’);
second.style.backgroundColor=”red”;
}

function changeBack2()
{
second = document.getElementById(‘Button2’);
second.style.backgroundColor=”blue”;
}

function changeIt3()
{
third = document.getElementById(‘Button3’);
third.style.backgroundColor=”orange”;
}

function changeBack3()
{
third = document.getElementById(‘Button3’);
third.style.backgroundColor=”blue”;
}

</script>

</head>

<body onload=”loadImage()”>

<img src=”americanflag.jpg” id=”imgContact” onMouseOver=”changeIt()” onMouseOut=”changeBack()”/><br /><br />

<input type=”button” value=”Button1″ id=”Button1″ color=”#ffffff” backgroundColor=”blue”><br /><br />

<img src=”americanflag2.jpg” id=”imgContact2″ onMouseOver=”changeIt2()” onMouseOut=”changeBack2()” /><br /><br />

<input type=”button” value=”Button2″ id=”Button2″ color=”#ffffff” backgroundColor=”blue”><br /><br />

<img src=”americanflag3.jpg” id=”imgContact3″ onMouseOver=”changeIt3()” onMouseOut=”changeBack3()” /><br /><br />

<input type=”button” value=”Button3″ id=”Button3″ color=”#ffffff” backgroundColor=”blue”>

</body>
</html>

[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@xelawhoApr 02.2012 — inline css is a little different from DOM styling, so you are looking for
[CODE]
style="background-color:blue"
[/CODE]

for your buttons

(if you wish to do them in inline css at all, which is not hugely advisable but not really harmful)

you can also simplify your code by using two just functions and passing them the relevant arguments:

[CODE]
<html>
<head>
<script type="text/javascript">

contactImage = new Image();
contactImage.src = 'americanpride.jpg';

contactImage2 = new Image();
contactImage2.src = 'baldeagle.jpg';

contactImage3 = new Image();
contactImage3.src = 'ProudToBeAmerican.jpg';

function loadImage()
{
document.getElementById('imgContact').src = contactImage.src;
document.getElementById('imgContact2').src = contactImage2.src;
document.getElementById('imgContact3').src = contactImage3.src;
}

function changeIt(changebutt,changecol)
{
first = document.getElementById(changebutt);
first.style.backgroundColor=changecol;
}

function changeBack(origbutt,origcol)
{
first = document.getElementById(origbutt);
first.style.backgroundColor=origcol;
}



</script>

</head>

<body onload="loadImage()">

<img src="americanflag.jpg" id="imgContact" onMouseOver="changeIt('Button1','green')" onMouseOut="changeBack('Button1','blue')"/><br /><br />

<input type="button" value="Button1" id="Button1" color="#ffffff" style="background-color:blue"><br /><br />


<img src="americanflag2.jpg" id="imgContact2" onMouseOver="changeIt('Button2','red')" onMouseOut="changeBack('Button2','blue')" /><br /><br />

<input type="button" value="Button2" id="Button2" color="#ffffff" style="background-color:blue"><br /><br />


<img src="americanflag3.jpg" id="imgContact3" onMouseOver="changeIt('Button3','orange')" onMouseOut="changeBack('Button3','blue')" /><br /><br />

<input type="button" value="Button3" id="Button3" color="#ffffff" style="background-color:blue"/>


</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@MichiKenauthorApr 02.2012 — Got it. Thank you.
Copy linkTweet thisAlerts:
@MichiKenauthorApr 02.2012 — Resolved
×

Success!

Help @MichiKen 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.18,
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,
)...