Search code examples
jqueryname-valuedynamic-values

Access dynamic(mysql) multiple field values of selected options using jquery or javascript


<select id="sub-category" name="scat[]" multiple="multiple" placeholder="Select SubCategories"><option val=""></option>';

I have used select2 jquery plugin for selecting mutiple categories. Now i need to access values of multi-selected options using jquery like below. Can anyone help me.

tag1= $("#sub-category[0]").val();
tag2= $("#sub-category[1]").val();
tag3= $("#sub-category[2]").val();
tag4= $("#sub-category[3]").val();

Solution

  • This will log the value of every selected option

    $(function(){
        $('#some_button').click(function(){
            $('#sub-category :selected').each(function(){
                console.log($(this).val());
            });
        });
    });