Adding new Option() to a select box doesn't work

R

Rob

Hi,
Is there anything wrong with this code or does new Option just not work
very well.
I have a select box on my form:
<select name="FirstLine" size="5" style="width:122px;">
</select>

and I have a form element:
<form METHOD="post" ACTION="Scale_New.asp" id="Scale_New"
name="Scale_New">

I'm trying to do something a little more complex but for the sake of
just getting this to work..I've simplified it and this doesn't work
either.

I have a button that calls a javascript function that adds "options" to
the select box.
<button name="FirstLine" onclick="Add1();">Add First Line</button>

and here's my javascript function:
function Add1()
{
document.Scale_New.FirstLine.options[0] = new Option('new text', 'new
value');

}
and I get an error "document.Scale_New.FirstLine.options is null or not
an object"

I've tried changing it to document.forms[0] and changing the options to
options[1] or even options[document.Scale_New.FirstLine.options.length]
and that doesn't work either.
Does anyone see anything wrong here?
I'm using IE 6.

Thanks
Rob
 
M

Martin Honnen

Rob wrote:

<select name="FirstLine" size="5" style="width:122px;">
</select>

<button name="FirstLine" onclick="Add1();">Add First Line</button>

If you need a name for that button then choose one different from the
name for the select. And adding type="button" is a good idea e.g.
<button name="buttonName" type="button" onclick="Add1();">

That should fix it, as least judging from the details you have provided.
If not post a URL.
 
R

Ray

Rob said:
Does anyone see anything wrong here?
I'm using IE 6.

Thanks
Rob

I tried your code unmodified, seems to run fine:

<html>
<head>
</head>
<script>
function Add1()
{
document.Scale_New.FirstLine.options[0] = new Option('new text', 'new
value');

}

</script>
<body>
<button name="FirstLine" onclick="Add1();">Add First Line</button>
<form METHOD="post" ACTION="Scale_New.asp" id="Scale_New"
name="Scale_New">
<select name="FirstLine" size="5" style="width:122px;">
</select>

</form>
</body>

</html>
 
T

Tim Slattery

Rob said:
Hi,
Is there anything wrong with this code or does new Option just not work
very well.
I have a select box on my form:
<select name="FirstLine" size="5" style="width:122px;">
</select>
<form METHOD="post" ACTION="Scale_New.asp" id="Scale_New"
name="Scale_New">
I have a button that calls a javascript function that adds "options" to
the select box.
<button name="FirstLine" onclick="Add1();">Add First Line</button>

and here's my javascript function:
function Add1()
{
document.Scale_New.FirstLine.options[0] = new Option('new text', 'new
value');

}
and I get an error "document.Scale_New.FirstLine.options is null or not
an object"

Your "button" and your "select" have the same "name" attribute:
"FirstLine". I'd guess that although you're trying to access the
select, you're getting the button instead. And the button, clearly,
doesn't have an "options" property.

So give them different names, or give the select group an "id"
attribute, then access it using "document.getElementById()".
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top