user3582852
user3582852

Reputation: 15

JQuery drop down select value in another dropdown

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

Answers (3)

Jack
Jack

Reputation: 8941

$(function(){
    var type = $('#type').change(function(){ 
        var def = type.find('option:selected').attr('default');
        $('#agegroup').val(def);
    });
});

http://jsfiddle.net/uGLZ4/4/

Same idea as the other answers but I cleaned up you code slightly.

Upvotes: 0

Johnston
Johnston

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'))
    })
})

http://jsfiddle.net/uGLZ4/3/

Upvotes: 0

sbonkosky
sbonkosky

Reputation: 2557

http://jsfiddle.net/uGLZ4/2/

$(function(){
    $('select#type').change(function(){ 
        var defaultVal = $(this).find(":selected").attr('default');
        $('select#agegroup').val(defaultVal);
    });
})

Upvotes: 1

Related Questions