Rod
Rod

Reputation: 15477

Select multiple elements

So I'm able to get multiple select elements and iterate thru them with the following:

$('select[name=sel1], select[name=sel2]').each(function(){

Well, I thought I could do something like below but doesn't work:

var sel1 = $('select[name=sel1]');
var sel2 = $('select[name=sel2]');
//Doesn't work
$('sel1, sel2').each(function () {
////Doesn't work either
$(sel1, sel2).each(function () {

Upvotes: 0

Views: 93

Answers (2)

user4227915
user4227915

Reputation:

This is an option too:

$.each([sel1, sel2, sel3, ...], function(key, val){
    /* val.on("event", ...  */
});

I did an example here.

You can search more about $.each() and arrays here.

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337714

You can use add() to append jQuery objects together:

var $sel1 = $('select[name=sel1]');
var $sel2 = $('select[name=sel2]');

$sel1.add($sel2).each(function () {
    // do something
});

Upvotes: 3

Related Questions