How do I show/hide any div element using jQuery or JavaScript when a different dropdown option is selected?
-
I am working on a wordpress site.. There is a <select> options and two div tag (and inside div tag, there are some html contents with different links). What I am trying to do is, when someone choose Option A from the dropdown list, it will show first div element and when someone choose Option B from the dropdown list, it will hide first div and will show second div element.. How I can do it? Please suggest me.. For example: Dropdown List: <select> <option >Red</option> <option selected disabled="Disabled">Blue</option> <option>Yellow</option> <option disabled="Disabled">Orange</option> <option>Green</option> </select> Div Tag: <div id="test1> .... some shortcodes of wordpress or html codes... </div> <div id="test2> .... some shortcodes of wordpress or html codes... </div> <div id="test3> .... some shortcodes of wordpress or html codes... </div> What I want is, by default, option red will be selected and first div test1 will be shown; but when someone choose option Yellow, it will hide div test1 and will show div test2 and when someone choose option Green, it will hide all and will show div test3.. and same, when someone will choose option Red, it will show div test1. Please help me out..
-
Answer:
Give the select an ID and the divs a class and add this code in the head of the page @http://jsfiddle.net/mplungjan/c7JLW/ $(function() { $("#sel1").on("change",function() { $(".hideable").hide(); var id = "#test"+(this.selectedIndex+1); $(id).show(); }).change(); });
Michel Plungjan at Quora Visit the source
Other answers
You can find the exact code @http://stackoverflow.com/questions/19395228/jquery-dynamic-hide-and-show-for-drop-down-menu#answer-19395386
Srihari Sankar Sahu
You can use jQuery "toggle()" method to do this.<select> <option value=""></option> <option value="Option1">Option1</option> <option value="Option2">Option2</option></select><div>This is a test</div><script> $(document).ready(function () { $("select").change(function () { $("div").toggle(); }); });</script>Here you can learn more about it. http://www.encodedna.com/2013/02/jquery-show-hide-div-animate-panel.htm.Thanks
Arun Kumar
This had code worked for me<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){$('#purpose').on('change', function() {if ( this.value == '1')//.....................^.......{$("#business_new").hide();$("#business").show();}else if ( this.value == '2'){$("#business").hide();$("#business_new").show();}else{$("#business").hide();}});});</script><body><select id='purpose'><option value="0">Personal use</option><option value="1">Business use</option><option value="2">Passing on to a client</option></select><div style='display:none;' id='business'>Business Name<br/> <br/> <input type='text' class='text' name='business' value size='20' /><input type='text' class='text' name='business' value size='20' /><br/></div><div style='display:none;' id='business_new'>Business Name<br/> <br/> <input type='text' class='text' name='business' value="1254" size='20' /><input type='text' class='text' name='business' value size='20' /><br/></div></body>Hope it will workhttp://sg.collectoffers.com/RedMart
Suneel Kumar Maurya
Gave an ID to Color Dropdown Bind change event of Color Dropdown On change event, get the selected Text Put condition on the selected Text of Dropdown and show/hide relative DIV I have implemented above in the following link http://jsfiddle.net/c7JLW/63/
Shahid Riaz Bhatti
Related Q & A:
- How can I remove duplicate Objects from array of Objects in javascript?Best solution by Stack Overflow
- How to bind an event to appended element in jQuery?Best solution by stackoverflow.com
- How can I show a link to my 360 profile?Best solution by Yahoo! Answers
- How can I sell something on ebay without using a credit card?Best solution by Yahoo! Answers
- How can I get cash on ebay by using a paypal?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.