Counting dynamic number of input elements on dom using jquery

How to read all input elements with id, starting with (myInputElem) inside dom using jquery. Thus, there will be a dynamic number of input elements such as

<input id="myInputElem1" type="text" value="Eleme1">
<input id="myInputElem2" type="text" value="Eleme2">
..
<input id="myInputElemx" type="text" value="Elemex">

So I just need to count them.

+4
source share
3 answers

You can use the attribute starting with the selector and then get the length

$('[id^="myInputElem"]').length
+12
source

Use an attribute begins with a selector ^.

If you want to use ends with a selector, use $

$("[id^='myInputElem']").length;
+4
source

Even you can try it

$('[value^="Eleme"]').length
0
source

Source: https://habr.com/ru/post/1584477/


All Articles