Page 1 of 1

[Javascript] Alert with multi-select options comma separated

PostPosted: Mon May 22, 2017 8:36 pm
by admin
Generating a comma delimited string from a multi select element in jQuery.

In this example we will take a multi-select option element and before page loads, create a popup alert that will list all the options comma separated (',').

Paste this code in the <head> and before the </head>:
Code: Select all

var arr = []; // create array

$('#cityTags').children().each(function() {
    arr.push($(this).text()); // add option text to array

alert(arr.join(', ')); // Brooklyn, NYC, USA,

Use this HTML as an example and paste within the <body> of the page:
Code: Select all
<select id="cityTags" multiple>