SinDesign
SinDesign

Reputation: 41

Show/Hide specific DIV when selecting an option from a dropdown using jQuery

I'm trying to setup a select dropdown from which, using jQuery, each option will display a specific DIV. I have most of the functionality working, I just can't get the displayed DIV to hide once another option is selected. This is my code so far:

<select id="contact-location">
    <option value="">-- Select Location --</option>
    <option value="sydneyBranch">Sydney</option>
    <option value="melbourneBranch">Melbourne</option>
</select>

<div id="sydneyBranch" style="display:none">
    CONTENT
</div>

<div id="melbourneBranch" style="display:none">
    CONTENT
</div>


$(document).ready(function() {
    $('#contact-location').change(function(){
        var location = $(this).val(),
        div = $('#' + location);

        if($(this).val(location)) {
            div.show();
        } else {
            div.hide();
        }
    });
});

Upvotes: 0

Views: 12950

Answers (4)

Adil
Adil

Reputation: 148110

Try this,

Live Demo

 $(document).ready(function() {
    $('#contact-location').change(function(){
        var location = $(this).val(),
        div = $('#' + location);

        $('div').hide();
            div.show();

    });
});​

EDIT

Currently the javascript will hide all the divs, we can prevent this if we assign some class to participating divs and replace $('div').hide(); statement with $('.somecssClass').hide();

<div id="sydneyBranch" class="somecssClass" style="display:none">
    CONTENT
</div>

<div id="melbourneBranch" class="somecssClass" style="display:none">
    CONTENT
</div>

Upvotes: 4

HADEV
HADEV

Reputation: 447

try this

<select>
        <option data-div-id="div1">Option 1</option>
        <option data-div-id="div2">Option 2</option>
        <option data-div-id="div3">Option 3</option>
    </select>

    <div id="div1" class="container"  style="display:none">1</div>
    <div id="div2" class="container"  style="display:none">2</div>
    <div id="div3" class="container"  style="display:none">3</div>

    $(document).ready( function() { 
        $("select").change(function(){
            var divId = $(this).find(":selected").attr("data-div-id");
            $(".container").hide();
            $("#" + divId).show();
        });
    });

Upvotes: 1

charlietfl
charlietfl

Reputation: 171679

add a common class to the content elements if you want them to be hidden again

DEMO: http://jsfiddle.net/q8G2N/

HTML:

   <div id="sydneyBranch" class="content" style="display:none">

JS:

$(function(){ /* shorthand $(document).ready(){*/

    $('#contact-location').change(function(){
        var location = $(this).val();
        /* don't do anything if blank option selected*/
         if( location !=''){
             $('.content').hide();
             $('#'+location).show();
         }

    });

})

Upvotes: 1

eddyuk
eddyuk

Reputation: 4190

This is what you looking for:

var currentShowing = undefined;

$(document).ready(function() {
    $('#contact-location').change(function(){
        var location = $(this).val(),
        div = $('#' + location);

        div.show();
        $(currentShowing).hide();
        currentShowing = div;

        if(currentShowing === undefined)
            div = currentShowing;
    });
});​

Upvotes: 0

Related Questions