[Javascript] Alert with multi-select options comma separated

Post a reply

In an effort to prevent automatic submissions, we require that you enter both of the words displayed into the text field underneath.
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Topic review

Expand view Topic review: [Javascript] Alert with multi-select options comma separated

[Javascript] Alert with multi-select options comma separated

Post by admin » Mon May 22, 2017 8:36 pm

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>