TheWandererr
TheWandererr

Reputation: 514

Add css to a dynamic added Tablerow

I am creating a Website using jquery mobile and on that website I got a table that looks like this:

FIDDLE

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<table data-role="table" class="ui-responsive ui-shadow gk-decorate" id="testTable" is="jqm-table">
  <thead>
    <tr>
      <th data-priority="1">Animal</th>
      <th data-priority="1">Sports</th>
      <th data-priority="1">Names</th>
      <th data-priority="1">Fruits</th>
      <th data-priority="1">Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Cat</th>
      <td>Football</td>
      <td>Anna</td>
      <td>Apple</td>
      <td></td>
    </tr>
    <tr>
      <th>Dog</th>
      <td>Baseball</td>
      <td>James</td>
      <td>Banana</td>
      <td>Checked</td>
    </tr>
    <tr>
      <th>Shark</th>
      <td>Hockey</td>
      <td>David</td>
      <td>Orange</td>
      <td>Checked</td>
    </tr>
    <tr>
      <th>Ape</th>
      <td>PingPong</td>
      <td>Locke</td>
      <td>Mango</td>
      <td></td>
    </tr>
  </tbody>
</table>

My question is: Can I somehow add a css class or css in general to the rows that got the String "Checked" inside the Status Coloumn.
I tried stuff like that:

var qTable = document.getElementById("testTable");
var row = qTable.tBodies[0].insertRow();
row.addClass() //error does not support addClass
row.css({..})//error does not support css..

NOTE: The table content gets added Dynamically

Upvotes: 0

Views: 547

Answers (3)

Razvan Zamfir
Razvan Zamfir

Reputation: 4686

Try:

$("#testTable tr").each(function(){
  var tds  = $(this).find("td");
  var statusCell = $(tds[3]);
  var statusCellTxt = statusCell.text();
  if (statusCellTxt.indexOf('Checked') !== -1){
    $(statusCell).css('border', '1px solid red');
  }
});

See fiddle HERE.

Upvotes: 1

Guruling Kumbhar
Guruling Kumbhar

Reputation: 1049

try this,

JS

$("#testTable td").each(function(){
    if($(this).text() =='StatusChecked'){
   $(this).parent().addClass('checked');
   }
});

CSS (for testing style only)

.checked{
  background:green;
}

updated fiddle -

https://jsfiddle.net/guruling/erc3ujzw/

Upvotes: 1

Vinay
Vinay

Reputation: 7674

Use contains: pseudo selector to filter out <td>s with your marker and then get their parent node (<tr>s)

$('td').filter(":contains('Checked')").parent().addClass('sugar')

https://jsfiddle.net/rttmq4we/

Upvotes: 1

Related Questions