1

I have this code:

<select>
    <option disabled selected>Select Method</option>
    <option value="question">Question</option>
    <option value="password">Password</option>
    <option value="email">Email</option>
    <option value="none">None</option>
</select>

Now I want when user select each of them some div that hidden in css with display:none; get visible for example when user select Question the div that have question id get visible or when Password selected the div that have password id get visible.
I try this but not work:

$(document).ready(function(){
    if ($("#auth option:selected").text() ==  "question"){
        $("#question").css("display","block");
    }
});

So how can I do this?

6
  • Bind to the change event of the <select> element, for a start. Commented Sep 9, 2014 at 9:27
  • 2
    how is possible someone to select from disabled dropdown? Commented Sep 9, 2014 at 9:28
  • why is this disable already ? Commented Sep 9, 2014 at 9:29
  • where is the #auth element Commented Sep 9, 2014 at 9:32
  • Ops, I forget to edit the code, it enable when user click on a checkbook. I fixed it. Commented Sep 9, 2014 at 9:32

4 Answers 4

2

If you follow the pattern you have done so far, you have to write code for each option. If your options and div elements are coupled with value and id, you can simply do like this,

$("select").change(function() {
    $("div").hide();
    $("#" + $(this).val()).show();
});

Demo Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Here is Demo for this , remove disabled from select tag so that user can select the options

Jsfiddle

http://jsfiddle.net/adarshkr/z9gcf40r/2/

Code

HTML

<select id="showdiv">
    <option disabled selected>Select Method</option>
    <option value="question">Question</option>
    <option value="password">Password</option>
    <option value="email">Email</option>
    <option value="none">None</option>
</select>

<div id="question" class="hide">
    <p>question</p>
</div>

<div id="password" class="hide">
    <p>password</p>
</div>

<div id="email" class="hide">
    <p>email</p>
</div>

<div id="none" class="hide">
    <p>none</p>
</div>

CSS

.hide{
    display:none
}

JAVASCRIPT

$("select").change(function(){
    $("div").hide();
    $("#"+$(this).val()).show();
});

Comments

0

try this,

$('#auth').on('change', function() {
    var that = $(this).val();
    if (that  ===  "question"){
        $("#question").css("display","block");
    }
});

1 Comment

@ale_x your welcome, you can upvote answer if it helped. Answer by Anoop Joshi is a good one though.
0

Better check value than the text.

$(document).ready(function(){
        if ($("#auth option:selected").val() ==  "question"){
            $("#question").css("display","block");
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.