javascript - Best way to alternate constraints on two dropdowns -


i have 2 dropdown selects have same values. building converter source (left) unit destination (right) unit . doesn't make sense convert , units. know how add event on dropdown select remove selected option alternate box unclear on writing produce best ui experience. give following trimmed case:

<select name="srcunit" id="srcunit">     <option value="unit 1">unit 1</option>     <option value="unit 2">unit 2</option>     <option value="unit 3">unit 3</option> </select>  <select name="dstunit" id="dstunit">     <option value="unit 1">unit 1</option>     <option value="unit 2">unit 2</option>     <option value="unit 3">unit 3</option> </select> 

what might best practice approaching this? if choose source of "unit 2" how should destination react, remove it? need reset button, or if choose "unit 2" in destination well, bounce source else (that seems worse , intuitive)?

forget , validate on submit?

check new value on each dropdown change event.

if value selected in other dropdown, move value of other dropdown next (or default) option.

to give user clue should not select same value twice, gray out values select in other dropdown.

i have written code you, here executable snippet:

$("#srcunit, #dstunit").change(function(){      /*detect wether have srcunit or dstunit */      if($(this).attr("id") == "srcunit"){var otherunit = "dstunit";}      else{var otherunit = "srcunit";}      /*save new value*/      var newval = $(this).children("option:selected" ).val();      /*select next option in other dropdown if it's old value selected*/      if(newval == $("#"+otherunit+" option:selected").val()){           $('#'+otherunit+' option:selected').removeattr('selected').next().attr('selected', 'selected');      }      /*update ui*/      $('#'+otherunit+' option').removeclass("disabled");      $('#'+otherunit+' option[value="'+newval+'"]').addclass("disabled");  });
.disabled {    color: #ccc;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <select name="srcunit" id="srcunit">      <option value="unit 1" selected>unit 1</option>      <option value="unit 2">unit 2</option>      <option value="unit 3">unit 3</option>  </select>    <select name="dstunit" id="dstunit">      <option value="unit 1" class="disabled">unit 1</option>      <option value="unit 2" selected>unit 2</option>      <option value="unit 3">unit 3</option>  </select>


i edited answer better fit question, old answer:

use html 5 disabled attribute.

<select name="srcunit" id="srcunit">     <option value="unit 1">unit 1</option>     <option value="unit 2" selected>unit 2</option>     <option value="unit 3">unit 3</option> </select>  <select name="dstunit" id="dstunit">     <option value="unit 1">unit 1</option>     <option value="unit 2" disabled>unit 2</option>     <option value="unit 3">unit 3</option> </select> 

read more , try out here: http://www.w3schools.com/tags/att_option_disabled.asp

subscribe onchange event of dropdowns javascript , toggle attribute needed.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -