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 08-10-2011, 03:36 PM   PM User | #1
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
changing the background color from the external js.js

I want to create a basic script where someone clicks the button and the document color is changing. while I can do it with inline even handlers and also using the script block in html document, i am not able to achieve the result using the external js.js. My goal is not to use any even handlers at all inside the html document. This is how my codes looks like:


<head>
<title> Basics Exercise</title>
<script type="text/javascript" src="js.js"></script>
</head>
<body>
<form>
<p> Click this button and the document's color will change again </p>
<input name="button" type="button" value="Change the background color of the document"/>
</form>



js.js has

function changecolor() {
document.bgColor='#ea0d17';
}

document.forms[0].button.onClick=changecolor();



I don't want anyone to write a script for me, but I would appreciate if someone told me why the script I wrote is not working.
chickentulip is offline   Reply With Quote
Old 08-10-2011, 03:55 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
I don't use the xhtml standard much myself, but I recall someone on this site mentioning that using the name attribute in xhtml has been deprecated, so perhaps if you referenced your targets by ID instead of name that would work?
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 08-10-2011, 03:58 PM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
name attributes are deprecated for forms(!) in xhtml. Normal form fields may and should still have names :-)

But: it is onclick and not onClick (case sensitive!) and it's changecolor and not changecolor()
Code:
document.forms[0].button.onclick=changecolor;
devnull69 is offline   Reply With Quote
Old 08-10-2011, 04:18 PM   PM User | #4
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
Thank very much for your responses. js is not an easy language to learn. Please help. My code still not working.

Devnull69-I changed the code as per your advice. but when I click the button -nothing happens.
DanInMa-referencing the button by Id -did not produce any result either.

the code looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Basics Exercise</title>
<script type="text/javascript" src="js.js"></script>
</head>
<body>
<form>

<input name="button" type="button" value="Change the background color of the document"/>
</form>
</body>
</html>




js file


function changecolor() {
document.bgColor='#ea0d17';
}

document.forms[0].button.onclick=changecolor;
chickentulip is offline   Reply With Quote
Old 08-10-2011, 04:21 PM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
You changed the bgColor attribute for the document. What you wanted to do is: Change the CSS style background-color for the body element
Code:
function changecolor() {
   document.body.style.backgroundColor = '#ea0d17';
}
devnull69 is offline   Reply With Quote
Old 08-10-2011, 04:40 PM   PM User | #6
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
i have inserted the code into html document . still no result. Error console tool tell me that document.forms[0] is underfined for some reason

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Basics Exercise</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript">
function changecolor() {
document.body.style.backgroundColor = '#ea0d17';
}
document.forms[0].button.onclick=changecolor;
</script>
</head>
<body>
<form action="">
<input name="button" type="button" value="Change the background color of the document"/>
</form>
</body>
</html>
chickentulip is offline   Reply With Quote
Old 08-10-2011, 05:58 PM   PM User | #7
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Yes, because it doesn't exist yet. HTML will be processed top-down (including Javascript) and Javascript will immediately be executed. By that time the form doesn't exist yet, because its HTML code has not yet been processed.

Solution: Wait until page has been loaded
Code:
window.onload = function() {
   document.forms[0].button.onclick = changecolor;
};
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
chickentulip (08-10-2011)
Old 08-10-2011, 06:56 PM   PM User | #8
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
Thank you!. I learned something new today
chickentulip 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 10:21 AM.


Advertisement
Log in to turn off these ads.