AVeryNiceWeb

[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.
Smilies
: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
<SCRIPT LANGUAGE="JavaScript">
javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.3.2",function($,L){

var arr = []; // create array

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

alert(arr.join(', ')); // Brooklyn, NYC, USA,
});
</SCRIPT>


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

Top

cron