Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting DOM elements based on ID values

Tags:

javascript

I have some textboxes in the DOM someting like

<INPUT id=txtQuantity587117 />
<INPUT id=txtQuantity587118 />
<INPUT id=txtQuantity587119 />
<INPUT id=txtQuantity587111 />
<INPUT id=txtQuantity587112 />

The elements are all nested and are not under any particular parent node.

Is there a way to select all the input elements with id text that starts with "txtQuantity".

I want a way with javascript and not with Jquery. Thanks.

like image 212
user1176058 Avatar asked Nov 20 '25 17:11

user1176058


1 Answers

In modern browsers (and also IE8... now verified) you can use document.querySelectorAll() the attribute-starts-with selector.

var els = document.querySelectorAll("input[id^=txtQuantity]");
like image 116
3 revsuser1106925 Avatar answered Nov 23 '25 07:11

3 revsuser1106925



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!