Full version: jsB@nk » Form » Dropdown » No Duplicate Selects
URL: https://www.javascriptbank.com/no-duplicate-selects.html
Prevent a selected option text or value from being chosen in another select box.
Full version: jsB@nk » Form » Dropdown » No Duplicate Selects
URL: https://www.javascriptbank.com/no-duplicate-selects.html
<script language="javascript">// Created by: Vic Phillips :: http://www.vicsjavascripts.org.uk/* The Select must have an 'onchange' event onchange="f1_NoDuplicateSelect(this,'text');" to verify that select texts are different or onchange="f1_NoDuplicateSelect(this,'value');" to verify that select values are different All variable, function etc. names are prefixed with 'f1_' to minimise conflicts with other JavaScripts */var f1_TextAry=new Array();var f1_ValueAry=new Array();function f1_NoDuplicateSelect(f1_obj,f1_type){ if (f1_obj.selectedIndex<1){ return; } if (!f1_obj.set){ f1_obj.set=true; if (f1_type=='text'){ f1_TextAry[f1_TextAry.length]=f1_obj; } if (f1_type=='value'){ f1_ValueAry[f1_ValueAry.length]=f1_obj; }} for (f1_0=0;f1_0<f1_TextAry.length;f1_0++){ for (f1_1=f1_0+1;f1_1<f1_TextAry.length;f1_1++){ if (f1_TextAry[f1_0].options[f1_TextAry[f1_0].selectedIndex].text==f1_TextAry[f1_1].options[f1_TextAry[f1_1].selectedIndex].text&&f1_type.toLowerCase()=='text'){ alert('Selection List Text\nMust be Different'); f1_TextAry[f1_1].selectedIndex=0; return false; } } } for (f1_2=0;f1_2<f1_ValueAry.length;f1_2++){ for (f1_3=f1_2+1;f1_3<f1_ValueAry.length;f1_3++){ if (f1_ValueAry[f1_2].options[f1_ValueAry[f1_2].selectedIndex].value==f1_ValueAry[f1_3].options[f1_ValueAry[f1_3].selectedIndex].value&&f1_type.toLowerCase()=='value'){ alert('Selection List Values\nMust be Different'); f1_ValueAry[f1_3].selectedIndex=0; return false; } } }}</script><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->
<select name="Color_1" class="TxtArea" onchange="f1_NoDuplicateSelect(this,'text');" > <option selected value=''>Select first color</option> <option value='1'>Red</option> <option value='2'>Yellow</option> <option value='3'>Green</option> <option value='4'>Blue</option></select><select name="Color_2" class="TxtArea" onchange="f1_NoDuplicateSelect(this,'text');" > <option selected value=''>Select second color</option> <option value='4'>Red</option> <option value='1'>Yellow</option> <option value='2'>Green</option> <option value='3'>Blue</option></select><select name="Color_3" class="TxtArea" onchange="f1_NoDuplicateSelect(this,'text');" > <option selected value=''>Select third color</option> <option value='3'>Red</option> <option value='4'>Yellow</option> <option value='1'>Green</option> <option value='2'>Blue</option></select><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->