I have a hidden input box from which I'm retrieving the comma-separated text value (e.g. 'apple,banana,jam'
) using:
var searchTerms = $("#searchKeywords").val();
I want to split the values up into an array, and then loop through the array.
split() method to convert a comma separated string to an array, e.g. const arr = str. split(',') . The split() method will split the string on each occurrence of a comma and will return an array containing the results.
Answer: Use the split() Method You can use the JavaScript split() method to split a string using a specific separator such as comma ( , ), space, etc.
To split a string with comma, use the split() method in Java. str. split("[,]", 0);
var array = $('#searchKeywords').val().split(",");
then
$.each(array,function(i){ alert(array[i]); });
OR
for (i=0;i<array.length;i++){ alert(array[i]); }
OR
for(var index = 0; index < array.length; index++) { console.log(array[index]); }
var array = searchTerms.split(","); for (var i in array){ alert(array[i]); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With