Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More than 1 value for each <option> in a select list 3

Status
Not open for further replies.

ntm0n

Programmer
Jun 22, 2001
30
US
Is it possible to have more than one value per option line in a list?

Ie.,

<select>
<option value=&quot;v1,v2&quot;> text </option>
</select>

What I'm trying to do is if a user selects the line, I need to be able to extract V1 and do something with it, and then V2 and do something with it...

Thanks

Gary

 
Let me expound on this a little...

Can the value of a Option selection be an array of two values? and if so... how would you set that?
 
I don't know the answer to your question, but I have a suggestion, in case what you're trying to do doesn't work. Perhaps you could set it equal to one string with delimiters in it, like value=&quot;v1|v2&quot;, then parse the value string to extract your 2 values. Don't know if it will work or not, but it's a thought.

--Ryan
 
Just create your own attributes in the option.

<Select ID=someSelect onChange='Dish()'>
<option attrOne=&quot;v1&quot; attrTwo=&quot;v2&quot; attrThree=&quot;v3&quot; value=&quot;whatever&quot;>Text</option>
</select>

then all you have to do is access the attributes of which ever option the user selects.

function Dish(){
var objAttrValue=document.getElementById(&quot;someSelect&quot;);
alert(objAttrValue.options(objAttrValue.selectedindex).attrOne);
alert(objAttrValue.options(objAttrValue.selectedindex).attrTwo);
alert(objAttrValue.options(objAttrValue.selectedindex).attrThree);

}
&quot;did you just say Minkey?, yes that's what I said.&quot;

MrGreed
 
gary,

I like your name BTW.

All your options are good for you. I do have a preference for snoopy's usage of the value field rather than more attributes. The reason for this is that you might send this form to a server and this would mean that the values inside the value attribute will also be on the server. Very useful. MrGreed's method is great. I love adding attributes myself but only use it when I know it is information useful on the client side only. His idea can be used in many other problems similar to this one so do keep it in a folder inside your mind. ;)

Extending on snoopy's method I'd like to bring to your attention the String.split() and Array.join() methods. They allow you to create an array from a string using a delimiter and do the reverse, a delimited string from an array. Here is an example :

var elementValue = &quot;value1|value2|etc&quot;;
var arrayValues = elementValue.split('|');

for(var ii = 0; ii < arrayValues.length; ii++)
{
alert(&quot;element at index &quot; + ii + &quot; is &quot; + arrayValues[ii])
}
// note you can also join elements of an array to make it a string delimited by a character
arrayValues[3] = &quot;this one was added at the last minute&quot;
var elementValue = arrayValues.join('|');
alert(&quot;the value string now contains : &quot; + elementValue);

I hope this helps. Gary Haran
 
Amazing...how three approaches all improving a little on the next has led me to an awesome outcome :) Thanks gentlemen I do appreciate the help!

Gary
 
Just a note for anyone else using this code, I had an undefined error popping up when attempting to get the .attrOne value... I found out by viewing the source that the attrOne variable name was being translanted to UPPER case giving me a ATTRONE... wierd but if you use the upper case version of the variable then it works great!

Gary

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top