blek
blek

Reputation: 11

Jquery on change situation

I have this code

$('#sel_kat').change(function() {
    $("#" + this.value).show();
});

#sel_kat is set in CSS to display: none;. When I select some value inside a <select> tag, it show me a div with id. How can I hide this div when I select another value?

Upvotes: 0

Views: 22

Answers (2)

Anonymous Duck
Anonymous Duck

Reputation: 2998

Or you can hide the last item selected

$('#sel_kat').change(function() {
    var lastSelect = $('#sel_kat:last').val();
    $("#" + lastSelect).hide();
    $("#" + this.value).show(); // show selected
});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337743

First, put a common class on all the div elements related to the select values. Then you can hide them all before showing the selected one:

$('#sel_kat').change(function() {
    $('.sel_div').hide(); // hide all
    $("#" + this.value).show(); // show selected
});

Upvotes: 1

Related Questions