Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-30-2012, 06:58 AM   PM User | #1
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
Simple background colour changing function not working

Code:
function tablea() {
	document.getElementById("tablea").background-color = "lightgray";
}
Code:
<A href="tables/a/" id="tablea" onclick="tablea()" style="background-color: gray">foobar</A>
Firefox's error console reports:
Quote:
Error: invalid assignment left-hand side
Source Code:
document.getElementById("tablea").background-color = "lightgray";
... pointing at the equals character.

I have no idea what that means, does anyone know what the issue is here?

Last edited by Phalanxer; 06-30-2012 at 07:07 AM..
Phalanxer is offline   Reply With Quote
Old 06-30-2012, 07:26 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
CSS properties with hyphens in them are represented by JavaScript properties in camelCase.

(CSS) background-color
should be (Javascript) backgroundColor - note the capital C


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Phalanxer (06-30-2012)
Old 06-30-2012, 08:06 AM   PM User | #3
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
Hey Phil,

Thanks, the error messages are gone, but the background colour isn't changing.

Last edited by Phalanxer; 06-30-2012 at 08:21 AM..
Phalanxer is offline   Reply With Quote
Old 06-30-2012, 08:32 AM   PM User | #4
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
Try it yourself and you'll see what I mean:

Code:
<STYLE type="text/css">
	A {color: black;}
</STYLE>
<SCRIPT type="text/javascript">
	function tablea() {document.getElementById("tablea").backgroundColor = "green";}
	function tableb() {document.getElementById("tableb").backgroundColor = "green";}
</SCRIPT>
<A href="a/" id="tablea" onclick="tablea()" style="background-color: red" target="table">111</A>
<A href="b/" id="tableb" onclick="tableb()" style="background-color: red" target="table">222</A>
<IFRAME name="table"></IFRAME>

Last edited by Phalanxer; 06-30-2012 at 08:41 AM..
Phalanxer is offline   Reply With Quote
Old 06-30-2012, 09:04 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
No surprise. You are assigning the same name to an id and a function.

id="tablea" onclick="tablea()"
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 06-30-2012, 09:11 AM   PM User | #6
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
I now have a function called 'tableafunction()' and an id called 'tablea'.

Code:
<STYLE type="text/css">
	A {color: black;}
</STYLE>
<SCRIPT type="text/javascript">
	function tableafunction() {document.getElementById("tablea").backgroundColor = "green";}
	function tablebfunction() {document.getElementById("tableb").backgroundColor = "green";}
</SCRIPT>
<A href="a/" id="tablea" onclick="tableafunction()" style="background-color: red" target="table">111</A>
<A href="b/" id="tableb" onclick="tablebfunction()" style="background-color: red" target="table">222</A>
<IFRAME name="table"></IFRAME>
Still doesn't work.

Last edited by Phalanxer; 06-30-2012 at 09:13 AM..
Phalanxer is offline   Reply With Quote
Old 06-30-2012, 09:22 AM   PM User | #7
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
I needed to put '.style' infront of .backgroundColor.

Solved.

EDIT: Having the same names wasn't a problem either... Suprise!

Last edited by Phalanxer; 06-30-2012 at 09:24 AM..
Phalanxer is offline   Reply With Quote
Old 06-30-2012, 09:31 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Phalanxer View Post
I needed to put '.style' infront of .backgroundColor.

Solved.

EDIT: Having the same names wasn't a problem either... Suprise!
It is very poor practice and will cause a crash in IE. Example:-

Code:
<input id = "text">

<script type = "text/javascript">
text = "Hello World";  // global variable
document.getElementById('text').value = text;
</script>

IE automatically reserves a var ID = domElement; in the global space for each DOM-Element with an ID. Some other browsers adopted this behaviour. So the same name for a javascript function /variable and an id will collide.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 06-30-2012, 09:32 AM   PM User | #9
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
I disagree with it being bad practice. I think its good practice, because it creates consistency which in turn makes it easier to work with.

LOL IE! I never ever think of IE when developing a website, only web standards. IE isn't even a web browser to me.
Thanks for your opinion though.

I am building an application that will be used by me only.
Phalanxer is offline   Reply With Quote
Old 06-30-2012, 09:35 AM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Phalanxer View Post
I am building an application that will be used by me only.
Well, that means you can break as many guidelines as you want. What you do in the privacy of your own home ....
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 06-30-2012, 09:36 AM   PM User | #11
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
Best practices aside, I will never rest until a page I am making is standard compliant in all languages that I am using - even if its just for me.
Phalanxer is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 08:14 AM.


Advertisement
Log in to turn off these ads.