jquery - Add css class in tr under condition -


i have table

<div class="container">     <div class="row">       <table id="mytable">         <thead>             <tr>                 <th>item id</th>                 <th>item name</th>                 <th>item price</th>             </tr>         </thead>         <tbody>             <tr>                 <td>1</td>                 <td>item 1</td>                 <td>$99</td>             </tr>             <tr>                 <td>2</td>                 <td>item 2</td>                 <td>$200</td>             </tr>         </tbody>       </table>     </div> </div> 

and want peform check, if value of column item price under 100 add class="warning in tr.

<tr class="warning">  <td>1</td>  <td>item 1</td>  <td>$99</td> </tr> 

how can jquery don't know jquery , try till unsuccessful.

$('#mytable tr/* how check here <100*/ ').addclass('warning'); 

you can use filter() here,

filter() : each element, if function returns true (or "truthy" value), element included in filtered set; otherwise, excluded. ( taken http://api.jquery.com/filter/ )

$('tbody tr').filter(function() {    return $('td:eq(2)', this)      // 3rd column using :eq(), :nth-child , :last or :last-child      .text()      // text content      .substr(1) * 1 < 100;    // number value string avoiding $ , compare    // if includes , use txt.replace(/[,$]/g,'')    // use parseint(txt,10) converting integer  }).addclass('warning');
.warning {    color: red  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div class="container">    <div class="row">      <table id="mytable">        <thead>          <tr>            <th>item id</th>            <th>item name</th>            <th>item price</th>          </tr>        </thead>        <tbody>          <tr>            <td>1</td>            <td>item 1</td>            <td>$99</td>          </tr>          <tr>            <td>2</td>            <td>item 2</td>            <td>$200</td>          </tr>        </tbody>      </table>    </div>  </div>


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -