Matching div id with with option select

Trying to match an option select with a div id containing the same name. My only problem is that I am unsure how to attribute the name of the selection into jQuery. The last line is where I am stuck.

$('#carlist').change(function(){
var chosen = $(this).val();
$('.cars').hide();
$('.cars').attr(''+chosen+'').fadeIn();
});

JS Fiddle below. https://jsfiddle.net/Lc4w2v3r/

I want to match the div id (for example, Jetta), with the option select of the exact same name.

Answers 1

  • You can select the target div (for example) with $('#Saab') and since you already got the variable chosen you can do something like this:

    $('#carlist').change(function(){
        var chosen = $(this).val();
        $('.cars').hide();
        $('#' + chosen).fadeIn();
    });
    

    JSFiddle


Related Articles