accessing selectbox value if name is variable

M

Michael

Hello!

I have a problem accessing the value of selected option. It works fine
if i put fixed name of select box, but not if the name is variable.
Can anyone help me with it?



function sel(name){
var p = 'prefix_'+name; // name is string like id0 or id1 and so on
var list = document.forms[0].p; //here fails the script in firefox,
because it cannot evaluate p as name of select box
var zonename = list.options[list.selectedIndex].value;
}





Thanks!
 
M

Michael

Hello!

I have a problem accessing the value of selected option. It works fine
if i put fixed name of select box, but not if the name is variable.
Can anyone help me with it?

function sel(name){
  var p = 'prefix_'+name; // name is string like id0 or id1 and so on
  var list = document.forms[0].p; //here fails the script in firefox,
because it cannot evaluate p as name of select box
  var zonename = list.options[list.selectedIndex].value;

}

Thanks!

function is called not directly in select tag, so I can't use 'this'
in call :
<input type="button" name="b" onclick="javascript:sel('id0');" />
 
M

Michael

I have a problem accessing the value of selected option. It works fine
if i put fixed name of select box, but not if the name is variable.
Can anyone help me with it?
function sel(name){
  var p = 'prefix_'+name; // name is string like id0 or id1 and so on
  var list = document.forms[0].p; //here fails the script in firefox,
because it cannot evaluate p as name of select box
  var zonename = list.options[list.selectedIndex].value;

Thanks!

function is called not directly in select tag, so I can't use 'this'
in call :
<input type="button" name="b" onclick="javascript:sel('id0');" />

second line in function should be:
var list = document.forms[0].elements[p];
 
G

Gregor Kofler

Laser Lips meinte:
Why don't you just do ...

var p = 'prefix_'+name;
list = document.getElementById(p);

Why should I use gEBI() when I have got the *name* of an element?

Gregor
 
G

Gregor Kofler

Michael meinte:

Forget about the "javascript:". What else than JS could be in the
handler attributes of an element?

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Michael meinte:

Forget about the "javascript:". What else than JS could be in the
handler attributes of an element?

In theory, code written in any scripting language. In practice, VBS(cript),
although probably only with MSHTML. For other UAs and script engines,
`javascript:' there this is only either a (useless) label or a syntax error.


PointedEars
 
G

Gregor Kofler

Thomas 'PointedEars' Lahn meinte:
In theory, code written in any scripting language. In practice, VBS(cript),
although probably only with MSHTML. For other UAs and script engines,
`javascript:' there this is only either a (useless) label or a syntax error.

Right. I mixed that up with the pseudo-protocol. In this case it would
be just - as you stated - label (but still wrong, because that was never
the intention of the OP).

Gregor
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top