jQuery code to get dropdown values in comma separated list

Yesterday I needed to get all the dropdown values and create a comma separated list. One way to iterate through all dropdown elements and create string. But I found there is another way to achieve this. Use jQuery $.map() which applies a function to each item in an array or object and maps the results into a new array.

$(document).ready(function() {
    var ddlvalues = $('#ddlCars option').map(function() {
        return $(this).val();
    });
    var ddlText = $('#ddlCars option').map(function() {
        return $(this).text();
    });
});?

See result below

See Complete Code

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...