Reputation: 15
I have two select boxes in my code, and I want the first select box to choose a default selection for the second box according to what is selected in the first.
The code is working except for the fact it looks at the value tag in the first select. I want it to look at the value in the default tag.
For example, if Computer Class is chosen, I want the second select box to immediately select "Adult."
One can see the full html and JQuery code on jsfiddle here: http://jsfiddle.net/uGLZ4/
This is my JQuery script:
$(function(){
$('select#type').change(function(){
$('select#agegroup').val($(this).val())
})
})
Upvotes: 1
Views: 133
Reputation: 8941
$(function(){
var type = $('#type').change(function(){
var def = type.find('option:selected').attr('default');
$('#agegroup').val(def);
});
});
Same idea as the other answers but I cleaned up you code slightly.
Upvotes: 0
Reputation: 20864
default
isn't valid but you can still technically do it:
$(function(){
$('select#type').change(function(){
$('select#agegroup').val($(this).find(":selected").attr('default'))
})
})
Upvotes: 0
Reputation: 2557
$(function(){
$('select#type').change(function(){
var defaultVal = $(this).find(":selected").attr('default');
$('select#agegroup').val(defaultVal);
});
})
Upvotes: 1