I have a piece of jQuery code that, when the user changes the value of a select, it copies the value selected down to other similar selects (same CSS class) that follow the one changed, if they don’t have a value already. Is there a more efficient way to do the following? It runs a bit sluggishly, at least in IE7.
$('.selectclass').change(function() {
if ($(this).val() != '') {
var baseElem = $(this);
$('.selectclass:gt(' + $('.selectclass').index(this) + ')').each(function (i) {
if ($(this).val() == '') {
$(this).val(baseElem.val());
}
});
}
});
This should be better optimised:
NB this is untested. If you could provide an HTML sample (e.g. jsfiddle) it could be tested more easily…
The efficiencies made here:
input.selectclassshould be faster than.selectclass, particularly in Internet Explorerthis.valuerather than creating a new jQuery instancevalrather than doing the looping manually witheach.