0

I have a bunch of elements on my site (dynamically created). E.g:

<input type="text" class="form-control" id="item5" name="item5">
<input type="text" class="form-control" id="item2" name="item2">
...

I do not know how many Items there are. I only know that the ID-Name is always "itemX" (X stand for a number that I don't know.)

On Button Click I want loop through the site and that it writes alle founded ID that begins wit "item..." in a text input. (hidden field)

Any Ideas, how I can do this?

DragonStyle
  • 145
  • 11
  • Possible duplicate of [How to get CSS to select ID that begins with a string (not in Javascript)?](http://stackoverflow.com/questions/11496645/how-to-get-css-to-select-id-that-begins-with-a-string-not-in-javascript) – trincot May 01 '16 at 11:09
  • how can a single input be both a hidden field and a text input? – gavgrif May 01 '16 at 11:17

1 Answers1

1

You can do it with attribute starts with selector,

$("#thatHiddenElement").val($("input[id^=item]").map(function(){ 
  return this.value 
}).get());

The above code will set the value in the hidden text box like "hai,hello"

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130