27

Here is my code. Why it doesn't work?

<Script> 
   $('#colorselector').change(function() {
        $('.colors').hide();
        $('#' + $(this).val()).show();
 });
</Script>
<Select id="colorselector">
   <option value="red">Red</option>
   <option value="yellow">Yellow</option>
   <option value="blue">Blue</option>
</Select>
<div id="red" class="colors" style="display:none"> .... </div>
<div id="yellow" class="colors" style="display:none"> ... </div>
<div id="blue" class="colors" style="display:none"> ... </div>
isherwood
  • 46,000
  • 15
  • 100
  • 132
yogsma
  • 9,106
  • 25
  • 86
  • 142
  • Possible duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Makyen Nov 24 '18 at 00:26

4 Answers4

82

You're running the code before the DOM is loaded.

Try this:

Live example:

http://jsfiddle.net/FvMYz/

$(function() {    // Makes sure the code contained doesn't run until
                  //     all the DOM elements have loaded

    $('#colorselector').change(function(){
        $('.colors').hide();
        $('#' + $(this).val()).show();
    });

});
user113716
  • 299,514
  • 60
  • 431
  • 433
  • Works and +1 for linking to jsfiddle.net – KP. Jun 04 '10 at 15:35
  • I have a small question. What should I do if I have to show same div section based on two or more select option values? I don't want to add additional div section. Like - Show red based on red and yellow values. – yogsma Jun 04 '10 at 16:13
  • @yogsma - You could simply give those ` – user113716 Jun 04 '10 at 16:28
  • what if it gets the value of the option "Red mango" and finds the div that has a title "Red mango", can this be possible??? – Francis Alvin Tan Apr 07 '13 at 01:08
  • beautifully simple. Works great – ChrisKsag Feb 26 '16 at 02:10
  • What do I change `.val()` to in order to get the class of the option instead of the value? Is it as simple as `.class()` or `.getclass()` ? – Paul Ruocco Oct 20 '16 at 15:46
  • thanks it is nice solution. @user113716 anyway how to do if my value is already selected then show div also. – pagol Aug 28 '17 at 10:43
17
<script>  
$(document).ready(function(){
    $('#colorselector').on('change', function() {
      if ( this.value == 'red')
      {
        $("#divid").show();
      }
      else
      {
        $("#divid").hide();
      }
    });
});
</script>

Do like this for every value

saurabh yadav
  • 499
  • 5
  • 11
  • 1
    Your solution is the best of all, as it gives full freedom to do anything easily. I removed the else statements and utilized only the if statements for flexibility. Published it on jsfiddle at [http://jsfiddle.net/FvMYz/6587/](http://jsfiddle.net/FvMYz/6587/) – Julius Aug 06 '18 at 20:05
3

To show the div while selecting one value and hide while selecting another value from dropdown box: -

 $('#yourselectorid').bind('change', function(event) {

           var i= $('#yourselectorid').val();

            if(i=="sometext") // equal to a selection option
             {
                 $('#divid').show();
             }
           elseif(i=="othertext")
             {
               $('#divid').hide(); // hide the first one
               $('#divid2').show(); // show the other one

              }
});
blue112
  • 41,908
  • 3
  • 40
  • 53
Summved Jain
  • 1,126
  • 19
  • 21
2

You are missing a :selected on the selector for show() - see the jQuery documentation for an example of how to use this.

In your case it will probably look something like this:

$('#'+$('#colorselector option:selected').val()).show();
Colonel Sponsz
  • 1,651
  • 1
  • 12
  • 21
  • 3
    No need to get the specific `option:selected`. Calling `.val()` on the select itself will return the value of the selected option. :o) – user113716 Jun 04 '10 at 15:39