Other Values Retrieved from Select

M

mike

I wanted to add additional data to my select function.

I tried:

<select id="sel_1" name="sel_1" onChange="dispSel(this);">
<option struct_id="24" value="4">Text 1</option>
<option struct_id="37" value="8">Text 2</option>
</select>

function dispSel(obj)
{
alert(obj.struct_id);
}

This did not work. I'd like to be able to add some other data
associated with the option in the select element.

Anyone have any other ideas.

One I thought of was instead of
<option struct_id="24" value="4">Text 1</option>
use
<option value="24|4">Text 1</option>

and then
function dispSel(obj)
{
var myarray=obj.value.split("|");
alert(myarray[0]);
}

Any help is appreciated.

Mike
 
E

Evertjan.

mike wrote on 13 mrt 2005 in comp.lang.javascript:
One I thought of was instead of
<option struct_id="24" value="4">Text 1</option>
use
<option value="24|4">Text 1</option>

and then
function dispSel(obj)
{
var myarray=obj.value.split("|");
alert(myarray[0]);
}

A good thought:

<select
onChange="alert(this.value.split(',')[1]);">
<option value="4,37">Text 4</option>
<option value="8,39">Text 8</option>
</select>
 
P

phil_gg04

<option struct_id="24"....
alert(obj.struct_id);

You might be able to get this to work using getAttribute, perhaps only
on some browsers. Look up attribute handling on Quirksmode. It is not
a good solution.
<option value="24|4">
var myarray=obj.value.split("|");
alert(myarray[0]);

Yes, this is a good way to do it.

Something that I thought of recently was this:

<select onchange="eval(this.value)">
<option value="javascript code here...">

The code in the value attribute could set variables, call functions
etc.
This might be a useful technique in a few situations.

--Phil.
 
R

RobB

mike said:
I wanted to add additional data to my select function.

I tried:

<select id="sel_1" name="sel_1" onChange="dispSel(this);">
<option struct_id="24" value="4">Text 1</option>
<option struct_id="37" value="8">Text 2</option>
</select>

function dispSel(obj)
{
alert(obj.struct_id);
}

This did not work. I'd like to be able to add some other data
associated with the option in the select element.

Anyone have any other ideas.

One I thought of was instead of
<option struct_id="24" value="4">Text 1</option>
use
<option value="24|4">Text 1</option>

and then
function dispSel(obj)
{
var myarray=obj.value.split("|");
alert(myarray[0]);
}

Any help is appreciated.

Mike

http://www.alistapart.com/articles/scripttriggers/
 
K

kdarling

I wanted to add additional data to my select function.
I tried:
<select id="sel_1" name="sel_1"
onChange="dispSel(this);">

It didn't work because you sent the entire select object, instead of
the selected option:

<select id="sel_1" name="sel_1"
onChange="alert(this.options[this.selectedIndex].struct_id);">
<option struct_id="24" value="4">Text 1</option>
<option struct_id="37" value="8">Text 2</option>
</select>

Kev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top