Avatar of Johny Bravo
Johny Bravo
 asked on

Change td color dynamically

Hi Experts,

I am creating a table from XML file with the help of Javascript.
Now I have a color attribute in my XML,and I want to set that color to the TD,where I am setting status.
//adding to the table
          var xmlDoc=null;
                if (window.ActiveXObject)
                {// code for IE
                xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                }
                else if (document.implementation.createDocument)
                {// code for Mozilla, Firefox, Opera, etc.
                xmlDoc=document.implementation.createDocument("","",null);
                }
                else
                {
                alert('Your browser cannot handle this script');
                }
                if (xmlDoc!=null)
                {
                xmlDoc.async=false;
                xmlDoc.load("ShipmentStat.xml");
 
                document.write("<table border='1' cellpadding='0'  cellspacing='0'>");
 
                var x=xmlDoc.getElementsByTagName("_x0040_Shipments");
for (i=0;i<x.length;i++)
                { 
                        
                document.write("<tr onMouseOut=\"this.className='normal'\"  onmouseover=\"this.className='highlight'\" border-bottom-color:'Black;' id='" + i + "'  onclick='trClick(this);'>"); 
                
                //document.write("<tr>");
                
                document.write("<td width ='200px'    >");
                document.write(
                //x[i].getElementsByTagName("Status")[0].childNodes[0].nodeValue);
                x[i].getAttribute("Status"));
                document.write("</td>");
 
                document.write("<td width ='300px'>");
                document.write(
                x[i].getAttribute("Carrier"));
                document.write("</td>");
                                
                 document.write("<td width ='350px'>");
                document.write(
                x[i].getAttribute("Origin_x0020_Address"));
                document.write("</td>");
                                
                 document.write("<td width ='350px'>");
                document.write(
                x[i].getAttribute("Destination_x0020_Address"));
                document.write("</td>");
                                 
                   document.write("<td width ='150px'>");
                document.write(
                x[i].getAttribute("Pickup_x0020_Date_x002F_Time"));
                document.write("</td>");
               
                   document.write("<td width ='150px'>");
                document.write(
                x[i].getAttribute("Delivery_x0020_Date_x002F_Time"));
                document.write("</td>");
                               
                   document.write("<td width ='100px'>");
                document.write(
                x[i].getAttribute("Equipment"));
                document.write("</td>");
                document.write("</tr>");
                }
                document.write("</table>");
                }
                //end adding table
                           </script>
 
 
here is the sample XML
<markers>
  <_x0040_Shipments Status="In-Transit[Delayed]" Color="brown" Origin_x0020_Address="1465 sunset hills road  Herndon,VA 20190" Latitude="38.9582" Longitude="-77.3418" Destination_x0020_Address="dublin dr  Lakewood,CA 90712" _x0020_Latitude="33.8489" _x0020_Longitude="-118.147" Carrier="Averitt Express Inc" Equipment="Dry Van" Pickup_x0020_Date_x002F_Time="03/09/2009 7:15 AM" Delivery_x0020_Date_x002F_Time="03/10/2009 7:30 AM" Miles="3615" ShipmentID="47" OriginLocId="190" DestLocId="233" Carrierid="24" equipid="1" />
  <_x0040_Shipments Status="Delivered[Delayed]" Color="yellow" Origin_x0020_Address="11465 Sunset Hills Road #400 REston,VA 20190" Latitude="38.9582" Longitude="-77.3418" Destination_x0020_Address="Add111  Herndon,VA 20170" _x0020_Latitude="38.9812" _x0020_Longitude="-77.3873" Carrier="ABF Systems" Equipment="Dry Van" Pickup_x0020_Date_x002F_Time="03/08/2009 8:00 AM" Delivery_x0020_Date_x002F_Time="03/10/2009 8:15 AM" Miles="3" ShipmentID="46" OriginLocId="72" DestLocId="96" Carrierid="19" equipid="1" />
  <_x0040_Shipments Status="" Color="yellow" Origin_x0020_Address="  Herndon,VA 20190" Latitude="38.9582" Longitude="-77.3418" Destination_x0020_Address="134 st l Rd  Crescent,MO 63025" _x0020_Latitude="38.51" _x0020_Longitude="-90.6428" Carrier="FedEx Freight" Equipment="ERTYII" Pickup_x0020_Date_x002F_Time="01/27/2009 7:00 AM" Delivery_x0020_Date_x002F_Time="01/27/2009 7:15 AM" Miles="0" ShipmentID="45" OriginLocId="221" DestLocId="131" Carrierid="27" equipid="14" />
  <_x0040_Shipments Status="" Color="yellow" Origin_x0020_Address="265 Clyde Morris Boulevard  Ormond Beach,FL 32174" Latitude="29.29" Longitude="-81.1238" Destination_x0020_Address="11465 sunset hills road  Reston,VA 20190" _x0020_Latitude="38.9582" _x0020_Longitude="-77.3418" Carrier="BAYOU STATE TRANSPORTATIO" Pickup_x0020_Date_x002F_Time="12/30/2008 8:45 AM" Delivery_x0020_Date_x002F_Time="01/10/2009 9:45 AM" Miles="1095" ShipmentID="44" OriginLocId="98" DestLocId="173" Carrierid="8" equipid="550" />
  <_x0040_Shipments Status="In-Transit[On time]" Color="gray" Origin_x0020_Address="265 Clyde Morris Boulevard  Ormond Beach,FL 32174" Latitude="29.29" Longitude="-81.1238" Destination_x0020_Address="11465 Sunset Hills Road #400 REston,VA 20190" _x0020_Latitude="38.9582" _x0020_Longitude="-77.3418" Carrier="FedEx Freight" Equipment="Dry Van" Pickup_x0020_Date_x002F_Time="12/18/2008 7:30 AM" Delivery_x0020_Date_x002F_Time="12/18/2008 7:30 AM" Miles="20" ShipmentID="43" OriginLocId="98" DestLocId="72" Carrierid="27" equipid="1" />
</markers>

Open in new window

HTMLJavaScript

Avatar of undefined
Last Comment
Johny Bravo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
gmrsecs

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Johny Bravo

ASKER
Thanks gmrsecs,
It works for me too.
quincydude

is it like this?
               document.write("<td width ='200px' style='background-color: " + x[i].getAttribute("Color") + "'" );
			   document.write(">");
                document.write(
                x[i].getAttribute("Status"));
                document.write("</td>");

Open in new window

Johny Bravo

ASKER
Yes quincydude,this too works.Thanks.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes