0

i have a table with values. some of values ends with specific string so i want to check whether input name attribute contains specific string using if loop.

i have tried using below code but it is not working:

$("#tabTax tbody tr").each(function () {
    if (this.name.endswith('taxtype')) {
        $(this).replaceWith('<tr id="rowTax' + $(this).find("input[name$='taxtype']").val() + '><td hidden="hidden"></td><td> ' + $(this).find("input[name$='taxtype']").val() + '(' + parseFloat($(this).find("input[name$='Taxperce']").val()) + '%)</td><td><input type="text" name= Bookingtax[' + rowcount + '].TaxAmount  disabled="disabled"  class="form-control chargesinputfield" value=' + ($(this).find("input[name$='Taxperce']").val() * sum / 100).toFixed(2) + '></input></td><td hidden="hidden"><input name=Bookingtax[' + rowcount + '].TaxID id=' + $(this).find("input[name$='TaxID']").val() + ' type="text" class="form-control" value=' + $(this).find("input[name$='TaxID']").val() + ' ></input></td><td hidden="hidden"><input name=Bookingtax[' + rowcount + '].taxtype id=' + $(this).find("input[name$='taxtype']").val() + ' type="text" class="form-control" value=' + $(this).find("input[name$='taxtype']").val() + ' ></input></td><td hidden="hidden"><input name=Bookingtax[' + rowcount + '].Taxperce id=' + $(this).find("input[name$='Taxperce']").val() + ' type="text" class="form-control" value=' + $(this).find("input[name$='Taxperce']").val() + ' ></input></td><td hidden="hidden"><input name=Bookingtax[' + rowcount + '].TaxAmount id=' + $(this).find("input[name$='TaxAmount']").val() + ' type="text" class="form-control" value=' + ($(this).find("input[name$='Taxperce']").val() * sum / 100).toFixed(2) + ' ></input></td></tr>')
        rowcount++;
    } else {
        $("#discountrate").replaceWith('<tr id="discountrate"><td> Discount</td><td hidden="hidden"></td><td><input type="text" name="Discountrate"  id="txtDiscountrate"  class="form-control chargesinputfield" value="' + $("#txtDiscountrate").val() + '"></input></td><td hidden="hidden"><input name=Bookingtax[' + rowcount + '].Taxperce id=' + $(this).find("input[name$='Taxperce']").val() + ' type="text" class="form-control" value=' + $(this).find("input[name$='Taxperce']").val() + ' ></input></td><td hidden="hidden"><input name=Bookingtax[' + rowcount + '].TaxAmount id=' + $(this).find("input[name$='TaxAmount']").val() + ' type="text" class="form-control" value=' + ($(this).find("input[name$='Taxperce']").val() * sum / 100).toFixed(2) + ' ></input></td></tr>')
    }
})  

I tried with this if (this.name.endswith('taxtype')), but it is not working.

Please help. Thanks in advance...

5
  • Please include all relevant code. Commented Sep 21, 2017 at 6:27
  • api.jquery.com/attribute-contains-word-selector Commented Sep 21, 2017 at 6:28
  • Your argument of the replaceWith function is so messy @@ Commented Sep 21, 2017 at 6:29
  • <input> has no closing tag.. Commented Sep 21, 2017 at 6:30
  • Typo. Its endsWith actually not endswith Commented Sep 21, 2017 at 6:39

1 Answer 1

3

Try use $(this).is("[name$=taxtype]")

This will check if the name ends with the string taxtype ( $= means ends with)

Demo

$("#tabTax tr").each(function() {
  if ($(this).is("[name$=taxtype]")) {
    console.log("name ends with taxtype")
    if ($(this).is("[name=taxtype]")) {
      console.log("this only match taxtype")
    }
  } else {
    console.log("name does not ends with taxtype")
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tabTax">
  <tr></tr>
  <tr name="taxtype"></tr>
  <tr></tr>
  <tr name="somethingtaxtype"></tr>
</table>

Sign up to request clarification or add additional context in comments.

4 Comments

I tried removing the $. It is still displaying same result.. Why is that?
Did you remove the first or last $ in $(this).is("[name$=taxtype]") ?
the $ in name..I tried some expirement with it and I got what it was.. I learned something new.. +1 from me :)
@ShadowFiend because my example just name is an exact match to taxtype. but if you add somethingtaxtype then it should show something else

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.