javascript - Hide div based on selected dropdown -
this code works fine when creating new page. selecting dropdown show or hide decorated markup. problem in edit page default selected id id 3 want div decorated 3 hidden on page load. @ sea javascript , jquery.
<div class="form-group"> <label class="control-label col-md-2" for="articlecategoryid">menu category</label> <div class="col-md-10"> <select class="chooseoption form-control" id="articlecategoryid" name="articlecategoryid"> <option value="1">pages</option> <option value="2">about</option> <option selected="selected" value="3">project</option> <option value="4">gallery</option> <option value="5">news</option> <option value="6">events</option> <option value="7">faqs</option> <option value="8">jobs</option> <option value="9">documents</option> <option value="10">clients</option> </select> <div class="form-group articlecategoryid 3"> <label class="control-label col-md-2" for="articleondate">start date</label> <div class="col-md-10"> <input class="form-control text-box single-line" data-val="true" data-val-date="the field start date must date." id="articleondate" name="articleondate" type="datetime" value="" /> <span class="field-validation-valid text-danger" data-valmsg-for="articleondate" data-valmsg-replace="true"></span> </div> </div> <div class="form-group articlecategoryid 1"> <label class="control-label col-md-2" for="artilceontime">on time</label> <div class="col-md-10"> <input class="form-control text-box single-line" id="artilceontime" name="artilceontime" type="text" value="" /> <span class="field-validation-valid text-danger" data-valmsg-for="artilceontime" data-valmsg-replace="true"></span> </div> </div>
below jquery snippet
<script type="text/javascript"> jquery(".optionname").hide(); jquery("document").ready(function () { /// have wait till after document loads run these things jquery("select.chooseoption").change(function () { jquery("." + this.id).hide(); var thisvalue = jquery(this).val(); if (thisvalue != "") jquery("." + thisvalue).show(); }); }); </script>
this being edit page want "<div class="form-group articlecategoryid 3">....</div>
hidden on page load since item id 3 has been selected other should show. appreciated
having components classes given such "articlecategoryid 1" bad idea.
you have sth like
<div class="form-group editable-category" data-category-id="1"> <label class="control-label col-md-2" for="artilceontime">on time</label> <div class="col-md-10"> <input class="form-control text-box single-line" id="artilceontime" name="artilceontime" type="text" value="" /> <span class="field-validation-valid text-danger" data-valmsg-for="artilceontime" data-valmsg-replace="true"></span> </div> </div>
and css
.editable-category{ display:none; } .editable-category.active{ display:block; }
and js follow
$(document).ready(function(){ $('select.chooseoption').on('change',function(){ var thisvalue = $(this).find('option:selected').val(); if(thisvalue){ $('.editable-category.active').removeclass('active'); $('.editable-category[data-category-id="'+thisvalue+'"]').addclass('active'); } }).trigger('change'); });
Comments
Post a Comment