Jquery reusable form toggle -
i'm trying create form has multiple dropdown selects, can toggle additional fields.
so, if dropdown 1 has "toggle" selected want "toggled field 1" appear , same thing dropdown 2. tried limiting selector adding various this
code.
$('.dropdowntoggle').change(function() { selection = $(this).val(); switch (selection) { case 'toggle': $('div.toggle').show(); break; default: $('div.toggle').hide(); break; } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <label>dropdown 1</label> <select class="dropdowntoggle" style="width: 67%;"> <option value="something">something</option> <option value="toggle">toggle</option> </select> <div class="toggle" style="display: none;"> <label>toggled field 1</label> <input type="text" placeholder="" style="width: 67%;"> </div> </div> <div> <label>dropdown 2</label> <select class="dropdowntoggle" style="width: 67%;"> <option value="something">something</option> <option value="toggle">toggle</option> </select> <div class="toggle" style="display: none;"> <label>toggled field 2</label> <input type="text" placeholder="" style="width: 67%;"> </div> </div>
use siblings: siblings of each element in set of matched elements, optionally filtered selector.
$(this).siblings(".toggle").show();
$('.dropdowntoggle').change(function() { selection = $(this).val(); switch (selection) { case 'toggle': $(this).siblings(".toggle").show(); break; default: $(this).siblings(".toggle").hide(); break; } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <label>dropdown 1</label> <select class="dropdowntoggle" style="width: 67%;"> <option value="something">something</option> <option value="toggle">toggle</option> </select> <div class="toggle" style="display: none;"> <label>toggled field 1</label> <input type="text" placeholder="" style="width: 67%;"> </div> </div> <div> <label>dropdown 2</label> <select class="dropdowntoggle" style="width: 67%;"> <option value="something">something</option> <option value="toggle">toggle</option> </select> <div class="toggle" style="display: none;"> <label>toggled field 2</label> <input type="text" placeholder="" style="width: 67%;"> </div> </div>
Comments
Post a Comment