Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery if element contains something, do stuff

I cannot get this to work. I simply want to display an alert for test purposes if the proposed td contains "OH".

 <table id="v65-cart-shipping-details">
     <tr>..</tr>
     <tr>..</tr>
     <tr>
       <td>United States, OH 43112</td>
     <tr>
  </table>

No matter what is in it (ie. United States, MI 48187) the alert still pops up, what am I missing?

$(document).ready(function() {
    if ($("#v65-cart-shipping-details tr:eq(2) td:contains('OH')")) {
      alert("Did Stuff") }  
});
like image 324
ToddN Avatar asked May 20 '26 22:05

ToddN


1 Answers

Because it will return true even if the element isn't found. Try this:

$(document).ready(function() {
    if ($("#v65-cart-shipping-details tr:eq(2) td:contains('OH')").length) {
      alert("Did Stuff") }  
});
like image 112
JNDPNT Avatar answered May 22 '26 11:05

JNDPNT