How do I redisplay the correct value option if I have duplicate value codes on a HTML Select Option dropdown?

BaffledRose
BaffledRose used Ask the Experts™
on
I have the following code in my HTML.  The problem is when I return from processing and want to re-display the selected text in the Select box, how do I determine which text matches the value??  The value codes cannot be altered.

Thank you for the help....

<SELECT name="TYP" value="">
    <OPTION value="       "> </OPTION>
    <OPTION  value="QBADGE ">Badge</OPTION>
    <OPTION  value="QBADGE ">Shield</OPTION>
    <OPTION value="QCREDEN">Credentials</OPTION>
   <OPTION value="QCREDEN"> Pass</OOPTION>
</SELECT>
 
<SCRIPT LANGUAGE=JAVASCRIPT type="text/javascript">
	LoadTyp("");
</SCRIPT>
 
function LoadTyp(vSelected)
{
	for(i=0; i <= document.forms[0].TYP.options.length - 1; i++)
	       if (document.forms[0].TYP.options[i].value == vSelected)
		{document.forms[0].TYP.options[i].selected = true;}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Look into comparing using the option.text property instead. It should return the text inside the option, instead of its value.

function LoadTyp(vSelected)
{
      for(i=0; i <= document.forms[0].TYP.options.length - 1; i++)
             if (document.forms[0].TYP.options[i].text == vSelected)
            {document.forms[0].TYP.options[i].selected = true;}
}
You may have to manipulate vSelected so that it contains the text as well, instead of the avlue. Obviously the above code won't work if vSelected is equal to "QBADGE", it has to be equal to something like "Badge" or "Shield".
Software Engineer
Commented:
the selectedIndex property of the select object indicates which was selected.

e.g.,




  var TYP = document.getElementsByName(  'TYP' );
  for ( var t = 0; t < TYP.length; t++ ) {
     var sel = TYP[ t ];
     if ( sel.nodeName == 'SELECT' ) {
       var SI = sel.selectedIndex;
       var val = sel.options[ SI ].value;
       var txt = sel.options[ SI ].text;
       alert( 'Selection: ' +  SI + '\n Value="' + val + '"\n Text = "' + txt + '"' );
     }
  }

Open in new window

Author

Commented:
Thank you very much for your help.  I used a mix of code to accomplish my goal.  Because of the way my pages are interfacing, I needed to combine the methods to get my achieved result.  It works great now!!
Thanks
HonorGodSoftware Engineer

Commented:
Super.  Glad to hear it.

Thanks for the grade & the points.

Good luck & have a great day.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial