Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting bgcolor of select box contd.

Status
Not open for further replies.

rmagan

Programmer
Jun 3, 2003
35
US
I have a select box in a table that is dynamically getting its options from the database. I have created a function that changes the BG color of the select box when the option value is changed. This works fine.

When the page is displayed, I want to initially set the BG color of the select box depending on the value from the database. The table is dynamic so I do not know how many rows will be displayed. I need to do this for ALL rows in the table. It was suggested to use onload, but how can I go thru all the elements.


<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function getColor(p_param_id) {
theSelect = document.getElementById(p_param_id)
theColor = theSelect.options[theSelect.selectedIndex].value
theSelect.style.backgroundColor = theColor.substr(1, theColor.length)
}
</SCRIPT>
<TABLE>
<SELECT name=&quot;p_att_status&quot; id=&quot;p_param1&quot; onChange=&quot;getColor(this.id)&quot;>'
<OPTION value=.WHITE>Present
<OPTION value=*RED>Absent
</SELECT>
<TABLE>
 
In order for you to change the background of each item, you will need to loop through each item that exists. To do this, you will want to call your function from the body tag using the onload event as follows.

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function getColor(p_param_id) {
theSelect = document.getElementById(p_param_id);
alert(theSelect.length)
for(var i=0;i<theSelect.length;i++)
{
theColor = theSelect.value;
theSelect.style.backgroundColor = theColor.substr(1, theColor.length);
}
}
</SCRIPT>
<body onload=&quot;getColor('p_param1')&quot;>
<TABLE>
<SELECT name=&quot;p_att_status&quot; id=&quot;p_param1&quot; <!--onChange=&quot;getColor(this.id)&quot;-->>'
<OPTION value=.WHITE>Present
<OPTION value=*RED>Absent
</SELECT>
<TABLE>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top