Computing.Net > Forums > Programming > Javascript help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Javascript help

Reply to Message Icon

Name: priti
Date: October 15, 2003 at 00:09:07 Pacific
OS: WIn 2000
CPU/Ram: P4
Comment:

Hi,
I'm beginner for javascript programming.
I'm trying to do change the color of the image(Rectangle) by prompting user to enter the color.
I have written following:
function changeColor(){
var temp=" ";
temp = prompt("Enter the color to change color of the rectangle:","Enter color");
shapecolor = temp;
[document.getElementByTagName("img").style.color = shapecolor;]

}
And in body part I've written following:
<body onLoad="preload();">
<form name="form1">

<table width="60%" align="left">
<tr>
<td width="40%" align="left" valian="left">

<input TYPE="BUTTON" VALUE="Change color" onClick="changeColor();"></p>
</td>
</tr>
</table>
</form>
</body>
</html>
This won't work, it gives me error on line between the brackets:
Object doesn't support this property or method!

Any help will be greatly appreciated.
thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: October 15, 2003 at 06:18:35 Pacific
Reply:

Priti-
Some of your post got swallowed by the parsing, but I think we get the general idea.

A couple of things:
1. You're trying to change the color of an image with javascript? This probably isn't the best way to go about this. A better bet is to make a DIV, TD, SPAN, etc. and just change its background color.

2. getElementByTagName(el) is probably inferior to getElementById(el), because the ID one you can have more than one of that kind of element and still know exactly which one you're accessing

3. valian should be valign

4. Why do you initialize "temp" to be a space? In javascript it's not necessary to declare temp at all, but it's probably good practice so you are in the habit in other languages.

I think that was all I saw. The following worked:
<HTML>
<HEAD>
<TITLE>test</TITLE>
<SCRIPT language="javascript">
function changeColor(){
var temp=" ";
temp = prompt("Enter the color to change color of the 
rectangle:","blue");
document.getElementById("myDIV").style.backgroundColor 
= temp;
}
</SCRIPT>
<STYLE>
div.rect {
height: 100px;
width: 100px;
background-color: red;
}
</STYLE>
</HEAD>
<body">
<form name="form1">
<table width="60%" align="left">
<tr>
<td width="40%" align="left" valign="left">
<DIV class="rect" ID="myDIV"></DIV>
<input TYPE="BUTTON" VALUE="Change color" onClick="changeColor();">
</td>
</tr>
</table>
</form>
</body>
</html>

Hope this helps,
-SN


0

Response Number 2
Name: priti
Date: October 15, 2003 at 08:25:37 Pacific
Reply:

Thank you so very much SN. You're geneous.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Javascript help

Javascript help www.computing.net/answers/programming/javascript-help/9864.html

Javascript Help www.computing.net/answers/programming/javascript-help/3967.html

Javascript help www.computing.net/answers/programming/javascript-help/14272.html