Change background color of 1st row in table

Below is my spry dataset table

Here is my dataset

var ds1 = new Spry.Data.XMLDataSet("/xml/data.xml", "rows/row"); 

Here is my jquery inside the method that is called by the click of a button

function addRow()
{
var newRow = new Array();
var nextID = ds1.getRowCount(); 

newRow['ds_RowID'] = nextID; 
newRow['id'] = "x"; 
newRow['name'] = "Abhishek";
newRow['country'] = "India";


ds1.dataHash[newRow['ds_RowID']] = newRow; 
ds1.data.push(newRow); 

Spry.Data.updateRegion(ds1); 

ds1.sort('name','descending'); 
ds1.setCurrentRow(newRow.ds_RowID);

$(".trEven td").css("background-color", "red");
alert($.fn.jquery);

/*$("#tableDg tbody tr:first").css({
    "background-color": "red"
});*/
}

Here is my table

<div id="cdiv" style="width:100%;" spry:region="ds1">
<table id="tableDg" 
    style="border:#2F5882 1px solid;width:100%;"  cellspacing="1" cellpadding="1"> 

    <thead>
     <tr id="trHead" style="color :#FFFFFF;background-color: #8EA4BB"> 
         <th width="2%"><input id="chkbHead" type='checkbox' /></th>
         <th width="10%" align="center" spry:sort="name"><b>Name</b></th> 
         <th width="22%" align="center" spry:sort="host"><b>Country</b></th> 

     </tr>
     </thead>

     <tbody spry:repeat="ds1">   
     <tr id="trOdd"   
     spry:if="({ds_RowNumber} % 2) != 0" onclick="ds1.setCurrentRow('{ds_RowID}');"
        style="color :#2F5882;background-color: #FFFFFF" class="{ds_OddRow}"> 
         <td><input type="checkbox" id="chkbTest" class = "chkbCsm"></input></td>
         <td width="10%" align="center">&nbsp;&nbsp;{name}</td> 
         <td width="22%" align="center">&nbsp;&nbsp;{country}</td> 


     </tr> 

     <tr id="trEven" 
     spry:if="({ds_RowNumber} % 2) == 0" onclick="ds1.setCurrentRow('{ds_RowID}');"
        style="color :#2F5882;background-color: #EDF1F5;" class="{ds_EvenRow}"> 
         <td><input type="checkbox" class = "chkbCsm"></input></td>
         <td id="tdname" width="10%" align="center">&nbsp;&nbsp;{name}</td> 
         <td width="22%" align="center">&nbsp;&nbsp;{country}</td> 

     </tr>
     </tbody>
     </table>
</div> 

I'm wrong something, please guide me. Thank:)

0
source share
3 answers

You should not use idfor odd and even lines. Values idmust be unique on the page.

So, Id offers:

<tr class="trOdd"

and

<tr class="trEven"

and then:

$(".trEven")

If you really want the first row in the body of the table to get a red background (unlike all even ones), your selector should be:

$("#tableDg tbody tr:first")
+1
source

Works for me (jsFiddle) . What problems do you have?

id's, - :

$('.trEven').each(function() {
    $(this).css({"background-color": "red"});
});

. : jQuery API -.each()

+2

If I remember correctly, <tr>describes the structure. <td>represents the visual part of the table. Or how some browsers do it.

Therefore, it $("#trEven td").css("background-color", "red")should work. And preferably, you should use classes instead of identifiers in cases where multiple instances may exist.

+2
source

All Articles