Ali L
Ali L

Reputation: 9

Get the total count of visible drodpdownlist and ids of all dropdownlist

I have a page and multiple select elements. Some are visible and some are not. I want to get the count of visible select and their respective Ids.

I'm getting the count using $("select:visible").length but I'm not able to get the visible dropdown Ids.

Upvotes: 0

Views: 43

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

I want to validate that page, pass the ids of all visible dropdowns and their values to a ajax function

In this case it would be best to build an array from the id and values of the visible selects. You can do that using map() before sending the data in your $.ajax request, something like this:

var selectData = $("select:visible").map(function() {
    return { 
        'id': this.id, 
        'value': this.value
    }
}).get();

$.ajax({
    url: '/myendpoint',
    data: selectData,
    success: function() {
        console.log('It worked!');
    }
});

Upvotes: 0

Dhara Parmar
Dhara Parmar

Reputation: 8101

1) Count of visible dropdownlist:

var len = $("select:visible").length;

2) to get ids of all visible dropdown list:

$("select:visible").each(function(i,v){ // loop through all visible selectbox
   console.log($(this).attr("id")); // you can get id using $(this).attr("id)
   // OR
   console.log(this.id);
})

Upvotes: 3

Related Questions