Jump to content

Problem Loading Colors


lvandazz
 Share

Recommended Posts

Hi guys am newbie in js scripting stuff anyway I wanted to load different colors in each table displayed but is sticks to the color i didn't even command . HEEEEELP!

<html><table bgcolor=FF3FCC cellpadding="4" border="2" align= center>	<tr>		<td bgcolor="red">		Javascript!		</td>	</tr></table></html><script type="text/javascript">var bcolor = new Array(); bcolor[0]="FFCC66"; bcolor[1]="CCFFCC"; bcolor[2]="FFFFCC"; bcolor[3]="CCCCFF"; var selector=0;document.write("<h1>Js</h1>");for(;selector<4;selector++){	document.write("<table border='1' cellpadding='8' cellspacing='3'  width='20%' height='10%' left='20%' bgcolor='0409FC'>");	document.write("<tr >");	document.write("<td bgcolor='bcolor[selector]'>");	document.write("Selector ",selector,".",bcolor[selector]);	document.write("</td>");	document.write("</tr>");	document.write("</table>");	//insert color	}</script>

Link to comment
Share on other sites

To start with there quite a few errors there, firstly, you shouldn't use the attribute bgcolor, if you do then you should use quotes around the values, and also you should include the # sign before the color code.Next you're using document.write after the ending html tag, so I believe this output wouldn't appear, depending on browsers on how bad they follow the w3c standards.And another problem is your for loop, you're missing a part out, try something like

for(selector=0;selector<4;selector++)

That is even if you set this to zero somewhere else.And again you're using document.write after the end of the HTML. Try putting all this JS inside the Body tags somewhere.And to get your colour, you'll need to do something like this:

	document.write("<td bgcolor='"+bcolor[selector]+"'>");

Because bcolor[selector] is a a variable, an array, you have to exclude it from the string part of the write method

Link to comment
Share on other sites

You can test it by pasting it in here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_introthe strange uncommanded color you was getting (dark red), was because the browser tried to find a color match for the color code: "bcolor[selector]", and it somehow found it's way to that color, it happened because you didn't give it the variable, you only gave it the string, if you download firebug on firefox, you can inspect the elements and see what and why they are being displayed like they are, try it out...

<html><table bgcolor="#FF3FCC" cellpadding="4" border="2" align= center>	<tr>		<td bgcolor="red">		Javascript!		</td>	</tr></table><script type="text/javascript">var bcolor = new Array(); bcolor[0]="#FFCC66"; bcolor[1]="#CCFFCC"; bcolor[2]="#FFFFCC"; bcolor[3]="#CCCCFF";document.write("<h1>Js</h1>");for(selector=0;selector<4;selector++){			document.write("<table border='1' cellpadding='8' cellspacing='3'  width='20%' height='10%' left='20%' bgcolor='#0409FC'>");	document.write("<tr >");	document.write("<td bgcolor='"+bcolor[selector]+"'>");	document.write("Selector "+selector+"."+bcolor[selector]);	document.write("</td>");	document.write("</tr>");	document.write("</table>");}</script></html>

Edited by djp
Link to comment
Share on other sites

Hi, the + in there is for joining the string, "<td bgcolor='"+bcolor[selector]+"'>"in this the selector is going to be a value from 0 to 3so it could be something likebgcolor[2]and that is:bcolor[2]="#FFFFCC";so when you do the:document.write("<td bgcolor='"+bcolor[selector]+"'>");in your page will be:<td bgcolor="#FFFFCC">hope this helps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...