1

In my code I've got pleny of inputs with the following pattern:

<input type="text id="Order_Products_0_quantity" value="0">
<input type="text id="Order_Products_1_quantity" value="1">
<input type="text id="Order_Products_2_quantity" value="2">

etc

The only difference between them is the number in the middle which stands for their place in the row. Is it possible to somehow match all of them and select their values with jQuery?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
andrzej541
  • 919
  • 1
  • 9
  • 21

3 Answers3

2

You can try using Attribute Starts With Selector [name^="value"].

Demo:

$('[id^=Order_Products_]').each(function(){
  console.log(this.value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="Order_Products_0_quantity" value="0">
<input type="text" id="Order_Products_1_quantity" value="1">
<input type="text" id="Order_Products_2_quantity" value="2">
Mamun
  • 66,969
  • 9
  • 47
  • 59
2

The correct way to do this would be:

<input type="text" data-order-qty="0" class="Order_Products_quantity" value="0">
<input type="text" data-order-qty="1" class="Order_Products_quantity" value="1">
<input type="text" data-order-qty="2" class="Order_Products_quantity" value="2">

You can then retrieve the element like so:

$(".Order_Products_quantity[data-order-qty=2]");

Or fetch the order qty like so:

$(".Order_Products_quantity").eq(1).attr('data-order-qty');

Here's more info on using custom attributes: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Matt
  • 158
  • 1
  • 10
  • 2
    Fairly certain the # in the class relates to which product the entry is for, not it's quantity. The value of the input would be the actual quantity. So your data field is slightly misleading. It would make more sense if it was `data-product="0"` – Taplar May 13 '20 at 17:12
  • *"The **correct** way"* - bzzt. Think you meant *"one possible way to do this"*. Still better to add a class. Or reference them from the parent element `$("#divwrapper>input")` – freedomn-m May 13 '20 at 17:14
  • Sure, there are many ways to do it. This way is just more correct than most of the other solutions. It's future proof, maintainable and quick. – Matt May 13 '20 at 17:18
1

Attribute selectors to the rescue:

  • Attribute Starts With Selector [name^=”value”]
  • Attribute Ends With Selector [name$=”value”]

var inps = $('input[id^="Order_Products_"][id$="_quantity"]')
console.log(inps.map(function () { return +this.value }).get())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="Order_Products_0_quantity" value="0">
<input type="text" id="Order_Products_1_quantity" value="1">
<input type="text" id="Order_Products_2_quantity" value="2">


<input type="text" id="Order_Products_0_foo" value="4">
<input type="text" id="Order_Products_1_foo" value="5">
<input type="text" id="Order_Products_2_foo" value="6">

It would be better to add a class, but this selector will work.

epascarello
  • 204,599
  • 20
  • 195
  • 236