javascript - Show/Hide using drop down list -
hello trying use code below. code want default div area 1. have html code showing div area 1 in drop down menu want javascript show div area 1 default. code?
<script type="text/javascript"> $(document).ready(function(){ $('.box').hide(); $('#dropdown').change(function() { $('.box').hide(); $('#div' + $('#dropdown').val()).show(); }); }); </script> <form> <select id="dropdown" name="dropdown"> <option value="0">choose</option> <option value="area1" selected="selected">div area 1</option> <option value="area2">div area 2</option> <option value="area3">div area 3</option> </select> </form> <div id="divarea1" class="box">div area 1</div> <div id="divarea2" class="box">div area 2</div> <div id="divarea3" class="box">div area 3</div>
$('.box').hide().first().show(); or:
$('.box').hide().filter("#divarea1").show(); put 1 of above in dom ready event:
$(function(){ /*...*/ }); or
$(document).ready(function(){ /* ... */ }); full code: (it should answer next question regarding how show selected div...)
$(document).ready(function() { $('.box').hide().filter("#divarea1").show(); $('#dropdown').change(function() { var selectedid= $(this).find(':selected').text().replace(/\s/g, "").tolowercase(); console.log(selectedid); $('.box').hide().filter('#' + selectedid).show(); }); });
Comments
Post a Comment