By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 2,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Sep 14 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 14 '06 #2

P: n/a
Ray

Rob wrote:
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>

Sep 14 '06 #3

P: n/a
Rob <ro****@hotmail.comwrote:
>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()".

--
Tim Slattery
Sl********@bls.gov
Sep 14 '06 #4

P: n/a
Rob
Ahhh. Thanks guys. Never even thought of that.

Rob

*** Sent via Developersdex http://www.developersdex.com ***
Sep 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.