Homer_J
Homer_J

Reputation: 3323

jQuery - function for select elements with same class only

I have the following code, which works fine:

$(function() {
    $('select').on('change', function(event){ // This binds listeners to the change event on all the select elements
        var sId = this.id; // store off the changed element id
        var vId = this.value; // store off the changed element value 
        var nId = this.name; // store off the changed element name 
        alert(sId + nId + vId); // for testing
        $('select').each( function(){ // this loops across the same set of elements
            if(this.name != nId && this.value == vId) { // If it is not the triggering element and the value is the same, do something
                this.options.selectedIndex = 0; // reset the value to 'rank' 
            }
        });
    });
});

This code works for all dropdown menus on a page - what I am trying to do is limit to only those dropdowns with the class dropdown_1 - is this possible?

Upvotes: 0

Views: 42

Answers (1)

palaѕн
palaѕн

Reputation: 73906

You can just change select to dropdown_1 in the code like:-

$(function () {
    $('.dropdown_1').on('change', function (event) { // This binds listeners to the change event on all the select elements
        var sId = this.id; // store off the changed element id
        var vId = this.value; // store off the changed element value 
        var nId = this.name; // store off the changed element name 
        alert(sId + nId + vId); // for testing
        $('.dropdown_1').each(function () { // this loops across the same set of elements
            if (this.name != nId && this.value == vId) { // If it is not the triggering element and the value is the same, do something
                this.options.selectedIndex = 0; // reset the value to 'rank' 
            }
        });
    });
});

Upvotes: 2

Related Questions