[jQuery] Why is the behavior only attached to the first select?
I'm trying to attach a function to all selects that have the custom
goto-attribute. It works for the first one, but the behavior isn't
being attached to the second <select>. Any ideas how to fix it?
$(document).ready(function() {
$('select[goto]').each(function () {
$(this).change(function () {
var selected = $("select option:selected").val();
if (selected != 'ignore') {
document.location = $(this).attr("goto") + '/' + selected;
}
});
});
});
// with html:
<select goto="www.cnn.com">
<option value="ignore">Choose...
<option value="finance">CNN Finance
<option value="sports">CNN Sports
</select>
<select goto="www.bbc.co.uk">
<option value="ignore">Choose...
<option value="finance">BBC Finance
<option value="sports">BBC Sports
</select>