getElementsByName with options

O

Otto Wyss

I've tried to access the value of a selected option through the
following getSelectValue function, passing the name of the select

function getSelectValue (name) {
var sel = document.getElementsByName(name)[0];
var i = sel.options.selectedIndex;
return i == -1? "": sel.options.value;
}

getSelectValue ('belongsto')
...
<select name="belongsto" ...

yet I always get the error "sel has no properties". Any idea what's
wrong? Is there a simpler way?

O. Wyss
 
P

Pete

function getSelectedIndex(label) {

retval = '';

var obj = document.getElementById(label);
if (obj) retval = obj.options[obj.selectedIndex].value;

return retval;

}
 
O

Otto Wyss

Thanks, albeit your using "getElementById" and not "getElementsByName".

var sel = document.getElementsByName('name')[0];

still retuns error "sel has no properties".

O. Wyss
function getSelectedIndex(label) {

retval = '';

var obj = document.getElementById(label);
if (obj) retval = obj.options[obj.selectedIndex].value;

return retval;

}
Otto said:
I've tried to access the value of a selected option through the
following getSelectValue function, passing the name of the select

function getSelectValue (name) {
var sel = document.getElementsByName(name)[0];
var i = sel.options.selectedIndex;
return i == -1? "": sel.options.value;
}

getSelectValue ('belongsto')
...
<select name="belongsto" ...

yet I always get the error "sel has no properties". Any idea what's
wrong? Is there a simpler way?

O. Wyss
 
A

ASM

Otto Wyss a écrit :
Thanks, albeit your using "getElementById" and not "getElementsByName".

I do not understand why you don't use forms tree ?!

function getSelectOptionChosen(form_name, select_name) {
var s = document.forms[form_name].elements[select_name];
if(s) return s.selectedIndex.value;
}
var sel = document.getElementsByName('name')[0];

it is not ('name') but (name)

Never give as name to an element : 'name'
still retuns error "sel has no properties".

Try giving an id to your select
and Pete's function would have to work
Pete said:
function getSelectedIndex(label) {
retval = '';
var obj = document.getElementById(label);
if (obj) retval = obj.options[obj.selectedIndex].value;
return retval;
}

But ... I think you make an error somewhere in calling your function,
because your function works fine

Copy-paste following to test :

<html>
<head>
<title>get element by name</title>
<script type="text/javascript">
function getSelectValue (name) {
var sel = document.getElementsByName(name)[0];
var i = sel.options.selectedIndex;
return i == -1? "": sel.options.value;
}
</script>
</head>
<body>
<a href="#" onclick="alert(getSelectValue('truc'));return false;">
what?
</a>
<form>
<select name="truc">
<option>
choice
</option>
<option value="01">
case 1
</option>
<option value="02">
case 2
</option>
<option value="03">
case 3
</option>
</select>
</form>
</body>
</html>
 
O

Otto Wyss

ASM said:
But ... I think you make an error somewhere in calling your function,
because your function works fine

Copy-paste following to test :
Thanks a lot, yet I can't see an error within the calling

<a href="javascript: window.location.href = 'test.php?val=' +
getSelectValue ('belongsto')" target=_top>...</a>
....
<select name="belongsto" ...>

O. Wyss
 

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
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top