Replace part of an input ID, Name and Class

Asked

Viewed 17 times

1

I have the following inputs

<div class="sub-item-rows" id="pega-id">

  <input class="numeric_only left addon_qty_limitador disabled" style="border:none; background-color:unset" maxlength="5" type="text" value="1" name="addon_qty_limitador[13][0]" id="addon_qty_limitador_13_0" disabled="disabled">
  <input class="numeric_only left addon_qty_limitador disabled" style="border:none; background-color:unset" maxlength="5" type="text" value="1" name="addon_qty_limitador[14][0]" id="addon_qty_limitador_14_0" disabled="disabled">
  <input class="numeric_only left addon_qty_limitador disabled" style="border:none; background-color:unset" maxlength="5" type="text" value="1" name="addon_qty_limitador[15][0]" id="addon_qty_limitador_15_0" disabled="disabled">   

  <input style="padding: 13px; border-radius:20px" type="submit" value="adicionar ao carrinho" class="add_to_cart green-button upper-text">
</div>

And the following function in Jquery:

$( document ).on( "click", ".add_to_cart", function() {        

     //Aqui preciso remover de todos os inputs o text _limitador
       form_submit('frm-fooditem');
   });

The function captures the click of the button, within this action need to check all the inputs that have the in the attribute name the text _limiter and remove this text from Name, ID and Class.

With the sample data, when clicking the button the code should be:

<div class="sub-item-rows" id="pega-id">

  <input class="numeric_only left addon_qty disabled" style="border:none; background-color:unset" maxlength="5" type="text" value="1" name="addon_qty[13][0]" id="addon_qty_13_0" disabled="disabled">
  <input class="numeric_only left addon_qty disabled" style="border:none; background-color:unset" maxlength="5" type="text" value="1" name="addon_qty[14][0]" id="addon_qty_14_0" disabled="disabled">
  <input class="numeric_only left addon_qty disabled" style="border:none; background-color:unset" maxlength="5" type="text" value="1" name="addon_qty[15][0]" id="addon_qty_15_0" disabled="disabled">   

  <input style="padding: 13px; border-radius:20px" type="submit" value="adicionar ao carrinho" class="add_to_cart green-button upper-text">
</div>
  • Just use the String.prototype.replace. You will "replace" the _limitador by an empty string.

  • sorry but I can’t quite understand why I needed to generate the id and name with "_limiter" and then remove, it was easier to generate the element as it should be, and change only the class for example. It’s not a common practice to change the elements' Id and Name, usually if you remove/add the whole element, but you can loop the elements and use replace, as @Luizfelipe commented, but this will mess up the DOM, use the attr or prop of jquery to prevent this

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.