...

View Full Version : background color



gencoglu
03-29-2010, 09:39 PM
Hi,

I have the following javascript codes.
I have two textboxes with id "pano" and "pano2"
My function creates numbers randomly and that if random number==1 let the background of textbox2 be pink and if random number==2
textbox1 be red. The code works fine for document.bgColor but it does not work for textboxes
how can I modify the code so that I can change the background color of any textboxes ?

thanks..





<script>
aaa=setInterval("letsgo()",500);
function letsgo()
{
rno=Math.floor(Math.random()*3)+1;
document.getElementById("counter").value=rno;
if(rno==3)
{
document.bgColor="green";
}
if(rno==2)
{
var pano=document.getElementById("pano");
document.pano.bgColor="red";

}
if(rno==1)
{
var pano2=document.getElementById("pano2");
document.pano2.bgColor="pink";

}
}
</script>
<input type="text" id="counter" style="position:absolute;top:10px;left:50px;width:60px;height:20px;">
<input type="text" id="pano" style="position:absolute;top:100px;left:100px;width:200px;height:200px;">
<input type="text" id="pano2" style="position:absolute;top:100px;left:400px;width:200px;height:200px;">

Old Pedant
03-29-2010, 09:44 PM
bgColor is a long obsolete property, anyway.

You should use something like

document.getElementById("pano2").style.backgroundColor = "pink";

gencoglu
03-29-2010, 09:49 PM
Thanks a ton OLDPEDANT

you did a great jop..and saved the day..

it works fine..
:thumbsup:

gencoglu
03-29-2010, 09:56 PM
Hi Old pedant

one more question.. what if I have checkbox instead of a text box.? How can I modify my code so if random number==1 let checkbox1 be checked and that if random number ==2 checkbox2 be checked.?

thanks in advanced

Old Pedant
03-29-2010, 10:05 PM
Will the checkboxes have the same name? Or different names?

gencoglu
03-29-2010, 10:10 PM
Will the checkboxes have the same name? Or different names?
different names

Old Pedant
03-29-2010, 10:48 PM
var which = Math.floor( Math.random() * 2 );
document.forms[0].firstCheckBoxName.checked = (which == 0);
document.forms[0].otherCheckBoxName.checked = (which == 1);

Just one way of doing it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum