Raunak Gupta
Raunak Gupta

Reputation: 10799

How to get all the selected option from multipal selectbox value using jQuery

I have a long dynamic form which has several Select Option like

<select class="common_dt_select" id="select_15" data-col-index="15">
   <option value="">All CC Status</option>
   <option value="0">Dead</option>
   <option value="1">Active</option>
   <option value="2">Frozen</option>
</select>

<select class="common_dt_select" id="select_23" data-col-index="23">
   <option value="">All</option>
   <option value="0">No</option>
   <option value="1">Yes</option>
</select>

I want to get all the selected option value and data-col-index value using jQuery.

I know I have to loop it by a common class so I have given common_dt_select but I cannot able to get the data.

How i want is

some loop that will run 
  if (sel_val != '') //I only want that value which is not blank
    console.log(sel_val);
    console.log(data_tag_id);
  end of if
end of loop

Upvotes: 0

Views: 56

Answers (2)

Derek Story
Derek Story

Reputation: 9593

Codepen

$('.common_dt_select').each(function() {
    var value = $(this).val();
    var colindex = $(this).data('col-index');

    if(value.length) {
        console.log(colindex);
        console.log(value);
    }
});

Upvotes: 1

Dakshika
Dakshika

Reputation: 1734

You can simply use below code

$('.common_dt_select :selected').each(function(i, sel){ 
  alert( $(sel).val()); 

 });

Upvotes: 0

Related Questions