jquery - TD :contains String but not within a span Element -


i want check wether td contains string if string not within span.

let's want check tds "treasure":

<td> treasure <span>water</span> </td> -> yes <td> water <span>treasure</span> </td> -> no <td> treasure </td> -> yes <td> <div>treasure</div> <span>water</span>  </td> -> yes 

so far have tried different things exclude span elements search wasn't successful.

$('table tr td:contains("treasure")').not('span').each(function() {     $(this).addclass('found'); }); 

thanks lot! :)

edit:

additionally following case should work:

<td> treasure <span>treasure</span> </td> -> yes 

you can use :not() pseudo-class selector (or not() method) :has() pseudo-class selector.

$('table tr td:contains(treasure):not(:has(span:contains(treasure)))').addclass('found');    // or    $('table tr td:contains(treasure)').not(':has(span:contains(treasure))').addclass('found');
.found {    color: red  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <table>    <tr>      <td>treasure <span>water</span>       </td>      <td>water <span>treasure</span>       </td>      <td>treasure</td>      <td>        <div>treasure</div> <span>water</span>       </td>    </tr>  </table>

update : if text contains in both place use filter() method. doing here cloning td , removing span elements cloned object after applying :contains selector.

$('table tr td').filter(function() {    return $(this)      .clone() // clone element      .find('span') // span inside cloned element      .remove() // remove spans      .end() // cloned element      .is(':contains(treasure)'); // check cloned element contains text  }).addclass('found');
.found {    color: red  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <table>    <tr>      <td>treasure <span>water</span>       </td>      <td>water <span>treasure</span>       </td>      <td>treasure</td>      <td>        <div>treasure</div> <span>water</span>       </td>      <td>        <td>treasure <span>treasure</span>        </td>    </tr>  </table>


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -