AVeryNiceWeb

[Javascript] Alert with multi-select options comma separated

[Javascript] Alert with multi-select options comma separated

Postby admin » Tue May 23, 2017 12:36 am

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>
The AVNW Support Team
http://www.AVeryNiceWeb.com

If you wish to add visual documentation (screenshots), make a donation and we'll let you know when the post has been updated


And, if our post has provided you with great information, there are a few ways you can show your appreciation:
  1. Like us on Facebook.- Our account had recently a hacking problem on FB and we were forced to create a new one. We lost all our posts & likes. :(
  2. Send us a testimonial.- Testimonials thanking for our good team work can be valuable to us and may encourage others to seek our support.
  3. Post a review.- Posting a good review about this site could also compel others to seek our advice & support.

Thank You!
User avatar
admin
Site Admin
 
Posts: 452
Joined: Mon Jan 13, 2014 3:18 am
Location: New York City

Return to Support

Who is online

Users browsing this forum: No registered users and 0 guests

cron