Bootstrap Tagsinput how to get multiple values as an array

8.8k Views Asked by At

Hey guys I have a problem with Bootstrap Tagsinput I'm trying to get an array with all the tags from the input text as values

<input type="text" name="designation" data-role="tagsinput" id="tags_id">

here's the js code

<script>
$('#tags_id').tagsinput('items');
</script>

When i insert some values in the input i get this "123,2131,12" but i need to get somthing like this ["123","2131","12"], and sorry for my english

1

There are 1 best solutions below

1
On
$('#tags_id').tagsinput('items').split(',');

since ',' is a delimiter we pass it to the split method which converts the string to an array.

Edit

Looking at the docs now , $('#tags_id').tagsinput('items') should return an array

$(document).ready(function(){  
  alert($('#input').tagsinput('items'));
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<script src="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>

<input id="input" type="text" value="Istanbul, Adana, Adiyaman, Afyon, Agri, Aksaray, Ankara" data-role="tagsinput" class="form-control" />

how are you debugging your data? when i use alert() the array seems to be serialized into a string. If so use a console.log to inspect it instead because everything seems fine