Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
change bg color of <div> by mouseover <li>
Old 07-03-2010, 10:59 AM change bg color of <div> by mouseover <li>
MattCoops's Avatar
Ultra Talker

Posts: 423
Name: Matt Cupan
Location: Charlotte, NC
Trades: 0
I have a <ul> containing 6 <li> (within its own <div>) floated to the left that correspond to 6 columns of <div>

I want the background-color of a <div> to display a different color onMouseover of its corresponding <li>

diagram attached tries to interpret the idea


any help or suggestive input is appreciated
Attached Images
File Type: jpg hover_change-bg.jpg (5.9 KB, 4 views)
__________________
Matt - Tweeting
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

read my
Please login or register to view this content. Registration is FREE
MattCoops is offline
Reply With Quote
View Public Profile Visit MattCoops's homepage!
 
 
Register now for full access!
Old 07-05-2010, 07:03 AM Re: change bg color of <div> by mouseover <li>
Kelpie's Avatar
Skilled Talker

Posts: 82
Name: Andrew
Location: SW Scotland
Trades: 0
An easy way to do this would be to have the div ids equal to an extension of the li ids, so a simple concatenate can form the relationship.

js:
Code:
<script type="text/javascript">
function colorSwitch(id, color) {
    element = document.getElementById('divFor' + id);
    element.style.background = color;
}
</script>
html:
Code:
<ul>
     <li id="ListItem1" onMouseOver="colorSwitch(this.id, '#ff9999');" onMouseOut="colorSwitch(this.id, '#ffffff');">Item 1</li>
     <li id="ListItem2" onMouseOver="colorSwitch(this.id, '#ff9999');" onMouseOut="colorSwitch(this.id, '#ffffff');">Item 2</li>
     <li id="ListItem3" onMouseOver="colorSwitch(this.id, '#ff9999');" onMouseOut="colorSwitch(this.id, '#ffffff');">Item 3</li>
</ul>

<div id="divForListItem1">
    <p>div 1</p>
</div>
<div id="divForListItem2">
    <p>div 2</p>
</div>
<div id="divForListItem3">
    <p>div 3</p>
</div>
Though if you want the divs to all have the same mouseOver and mouseOut background colors, the code can be tidied up a bit

js:
Code:
<script type="text/javascript">
function colorSwitch(id) {
    element = document.getElementById('divFor' + id);
    element.style.background = '#ff9999';
}
function colorRevert(id) {
    element = document.getElementById('divFor' + id);
    element.style.background = '#ffffff';
}
</script>
html for list (code for divs is as above):
Code:
<ul>
     <li id="ListItem1" onMouseOver="colorSwitch(this.id);" onMouseOut="colorRevert(this.id);">Item 1</li>
     <li id="ListItem2" onMouseOver="colorSwitch(this.id);" onMouseOut="colorRevert(this.id);">Item 2</li>
     <li id="ListItem3" onMouseOver="colorSwitch(this.id);" onMouseOut="colorRevert(this.id);">Item 3</li>
</ul>

Last edited by Kelpie; 07-05-2010 at 07:23 AM.. Reason: Additional info
Kelpie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to change bg color of <div> by mouseover <li>
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16399 seconds with 13 queries