jquery - Displaying inputs based on previous selection -
i have page several select inputs take following form
<div class="row"> <div class="col-md-12"> <div class="form-group"> <div class="col-sm-7"> <select class="selectpicker" name="producttype" id="producttype"> <option value=""></option> <option value="cereal">cereal</option> </select> </div> </div> </div> </div>
what trying display select inputs when previous select has been selected. doing
$( "#producttype" ).change(function() { if($(this).val() != '') { $('#yearrow').css('display', 'block'); } else { $('#yearrow').css('display', 'none'); } });
i have set working jsfiddle
although have works, have feeling going ugly. reason because if select first input, , second, , unselect first, third input remain. handle display of every input within every select change function, think messy. basically, want avoid this
$( "#producttype" ).change(function() { if($(this).val() != '') { $('#yearrow').css('display', 'block'); } else { $('#yearrow').css('display', 'none'); $('#monthrow').css('display', 'none'); $('#idrow').css('display', 'none'); //any other rows } });
is there better way of doing attempting do?
thanks
you can try this:
- add class selects ,
row
parents.
then can use select's class selector this:
$( ".selectpicker" ).change(function() { var $this = $(this); //display next select if ($this.val().tostring().length) { $this.closest('div.select-parent').next('div.select-parent').removeclass('hiddenrow'); return; } //hide next selects $this.closest('div.select-parent').nextall('div.select-parent').addclass('hiddenrow') .find('select').val("").selectpicker('refresh'); //refresh select });
here fiddle
.
Comments
Post a Comment