Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

referencing multiple values inside an option element

Similar question to Can an Option in a Select tag carry multiple values? - except I want to reference the values in JavaScript

To recap:
I want to return more than one value for a selected item in a selectbox - so I was thinking something like this:

<SELECT id='selectList'>
   <OPTION value1="a" value2="b" value3="c">OPTION 1</OPTION>
   <OPTION value1="d" value2="e" value3="f">OPTION 2</OPTION>
</SELECT>

I then want to get the values of the selected item:

var sel = document.getElementById('selectList');
selValue1 = sel.options(sel.selectedIndex).value1;
selValue2 = sel.options(sel.selectedIndex).value2;
selValue3 = sel.options(sel.selectedIndex).value3;

currently I can pull value='x' for an option but I need more values to be associated with the option. I could probably delimit all three values into one and later use string functions to split them out, but that just seem nasty.

like image 551
user2213361 Avatar asked Aug 31 '26 16:08

user2213361


1 Answers

I think you are looking for the attributes property:

var sel = document.getElementById('selectList');

selValue1 = sel.options[sel.selectedIndex].attributes.value1.value;

Here is a jsFiddle demonstrating how to extract these values.

like image 78
Josh Avatar answered Sep 03 '26 04:09

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!