473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

getElementsByName with options

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[i].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
--
Cross-platform applications:
http://wyoguide.sf.net/index.php?page=projectlist.php
Jan 11 '07 #1
4 21520
function getSelectedIndex(label) {

retval = '';

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

return retval;

}
Otto Wyss wrote:
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[i].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
--
Cross-platform applications:
http://wyoguide.sf.net/index.php?page=projectlist.php
Jan 12 '07 #2
Thanks, albeit your using "getElementById" and not "getElementsByName".

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

still retuns error "sel has no properties".

O. Wyss

Pete wrote:
function getSelectedIndex(label) {

retval = '';

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

return retval;

}
Otto Wyss wrote:
>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[i].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
--
Cross-platform applications:
http://wyoguide.sf.net/index.php?page=projectlist.php

--
Cross-platform applications:
http://wyoguide.sf.net/index.php?page=projectlist.php
Jan 14 '07 #3
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 wrote:
>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[i].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>
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jan 14 '07 #4
ASM wrote:
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

--
Cross-platform applications:
http://wyoguide.sf.net/index.php?page=projectlist.php
Jan 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simon | last post by:
<html> <head> <script language="vbscript"> sub window_onload msgbox document.getElementsByName("name1").length end sub </script> </head> <body> <div name="name1">testDiv</div>
21
by: briggs | last post by:
<html> <head> <script> /* AddChild */ function ac() { var c; var p = document.getElementById("p"); for (var i = 0; i < 5; i++) { c = document.createElement("DIV"); // Create 'div' element.
1
by: acord | last post by:
Hi, I want to use document.getElementsByName('name').value to retrieve a value of the field 'name', but it returns undefined. But function document.xxxform.name.value returns the correct value....
2
by: André Wagner | last post by:
I'm trying to get all the "divs" that have a given NAME using getElementsByName(). For example, the following code: <html> <head> <script type="text/javascript"> function on_load() { var...
22
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I...
4
by: david.kuczek | last post by:
I got the following error in a javascript I wrote. The script works fine, but why is the error being displayed??? ##### Here comes the little script: function...
2
by: tsalm | last post by:
Hi everybody, I need to get a element create dynamically by its name. It's ok for FF, but on IE, getElementsByName return "undefined". I create this code but not sure that it can work on older...
1
by: Sebarry | last post by:
Hi, Can anyone tell me how to use getElementsByName to retrieve the length of a HTML select input type? I've tried document.getElementsByName("selectName").options.length but it doesn't work. ...
14
rrocket
by: rrocket | last post by:
I am passing a value through query string to a popup and then trying to pass it back to the main page... I store the value in a hidden input box <input type="text" name="FromZip" id="FromZip"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.