Codded
Codded

Reputation: 1256

javascript/jquery combine hidden inputs as var

I have a list of tags which all get added to a hidden input with name="item[tags][]"

<input type="hidden" style="display:none;" value="first" name="item[tags][]">
<input type="hidden" style="display:none;" value="second" name="item[tags][]">
<input type="hidden" style="display:none;" value="third" name="item[tags][]">

How could i combine those hidden inputs to output

var tag_filter = first,second,third;

Upvotes: 1

Views: 61

Answers (2)

nandu
nandu

Reputation: 779

Try some thing like this

var str = "";
$('input[name="item[tags][]"]').each(function(){
    str += $(this).val()+","
})
str = str.substring(0,str.length);

Upvotes: 0

Adil
Adil

Reputation: 148110

You can use name selector to access the input elements and use map function along with get and join to get the comma separated list of values.

Live Demo

var tag_filter = $('[name="item[tags][]"]').map(function(){
  return this.value;
}).get().join();

Upvotes: 3

Related Questions