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.

CSS Forum


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



Reply
Javascript override css background-color
Old 09-05-2007, 02:25 PM Javascript override css background-color
Extreme Talker

Posts: 182
Trades: 0
I have the background color of an element set in an external style sheet. I am trying to use javascript to change the background color of this element on mouseover. For some reason, the javascript will not override the css in the external style sheet.

My code is something like this:

Code:
styles.css:

#mydiv
{
  background-color:#FFFFCC;
}

mypage.html:

<div id="mydiv" onmouseover="style.backgroundColor='#FFFFFF';">
   div content
</div>
The mouseover works fine if I don't set the background-color in the style sheet.

How do I fix this?
bhgchris is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-05-2007, 02:45 PM Re: Javascript override css background-color
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,935
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
onmouseover="style.backgroundColor = '#FFFFFF'" onmouseout="style.backgroundColor='#FFFFCC'"

Non-JS users will see an FFFFCC colored background and JS users will have a background color depending on state.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 09-05-2007, 02:54 PM Re: Javascript override css background-color
Extreme Talker

Posts: 182
Trades: 0
style.backgroundColor = '#whatevercolor' isn't overriding the background-color that is set in my style sheet
bhgchris is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 02:55 PM Re: Javascript override css background-color
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,935
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
Try this.style.backgroundColor...sorry, just missed that part.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 09-05-2007, 03:01 PM Re: Javascript override css background-color
Extreme Talker

Posts: 182
Trades: 0
tried.

I've tried it about 5 different ways, including using prototype and setStyle()...no dice =/
bhgchris is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 03:15 PM Re: Javascript override css background-color
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,371
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
function setColour(colCode,objID) {
document.getElementById(objID).style.backgroundCol or = "#" + colCode;
}


onmouse[event]="setColour('ffffcc',this.id)

should work
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-05-2007, 03:17 PM Re: Javascript override css background-color
Extreme Talker

Posts: 182
Trades: 0
tried that too. Again, works as long as I don't set the background-color in the external sheet. I can set it with the htm style attribute and it gets overwritten just fine
bhgchris is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 03:19 PM Re: Javascript override css background-color
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,935
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
There's got to be something else interfering then, Chris.

How about you post the page you're working on? Anything Hirst or I suggested should work.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 09-05-2007, 03:31 PM Re: Javascript override css background-color
Extreme Talker

Posts: 182
Trades: 0
No problem. I'll post some of my exact code. Have you tried these examples you have given? I've tested in FF and IE and no bueno.

snipped from my css file:
Code:
#new td
{
	background-color:#99CCFF;
}
snippet from my html template ({bgcolor} is either #efefef or #ffffff):
HTML Code:
		<tr id="new" onmouseover="bgcolor(this,'#FFFFCC');" onmouseout="bgcolor(this,'{bgcolor}');">
			<td id="date">{date}</td>
			<td id="from">{from}</td>
			<td id="message">{message}</td>
			<td id="reply">--</td>
			<td id="delete"><input type="checkbox" name="d[]" value="{id}" /></td>
		</tr>
my js function bgcolor() ( i have tried writing this function with getElementById and many other different ways, this is just the most recent way i've tried ):
Code:
function bgcolor ( what, color )
{
	what.style.backgroundColor = color;
}

I will note again that if I don't use the stylesheet to set the background-color, this function works perfect.
bhgchris is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 03:46 PM Re: Javascript override css background-color
Extreme Talker

Posts: 182
Trades: 0
ok, I just figured out what the problem was.

If I take the 'td' out of the style sheet, it works well enough.

HTML Code:
#new
{
  background-color:#99CCFF;
}
I would like to know why it wasn't working w/ the 'td' in there though? Any ideas?
bhgchris is offline
Reply With Quote
View Public Profile
 
Old 09-07-2007, 01:30 AM Re: Javascript override css background-color
Junior Talker

Posts: 1
Name: J
Trades: 0
Quote:
Originally Posted by bhgchris View Post
ok, I just figured out what the problem was.

If I take the 'td' out of the style sheet, it works well enough.

HTML Code:
#new
{
  background-color:#99CCFF;
}
I would like to know why it wasn't working w/ the 'td' in there though? Any ideas?
You defined a name and didn't use it.
#new td{...} != <tr="new"...>

As a general rule of thumb, don't use blank spaces in name definitions in any programming language.
Name it #newTD, #new_TD or something of the sort.
winterwill is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Javascript override css background-color
 

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 1.02039 seconds with 12 queries