473,320 Members | 2,071 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,320 software developers and data experts.

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

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
4 3040


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
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
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
Rob
Ahhh. Thanks guys. Never even thought of that.

Rob

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

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

Similar topics

3
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the button is clicked. However, that table to which a...
3
by: Iain Hallam | last post by:
Hi. I've been using display:none on the style property of some <option> elements in my forms, which works fine with Mozilla - as expected it removes the option from my dropdown (although it...
6
by: Scott | last post by:
The code below appears to work on the following: MAC - Safari PC - IE PC - Opera But the addition of items to the dropdown (select2) does not function in: MAC – IE
10
by: David | last post by:
Can anyone give me a quick code snippet (that is standards-based) for adding OPTION tags to a SELECT dynamically. I have no problem doing it in IE but I am kind of new to the whole standards world...
4
by: domas | last post by:
Hi, I can't get my semi-dynamic form to work. Maybe you could help. I've got a simple form with a select box. And a button that, when clicked, creates another copy if the select box and places...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
5
by: consonanza | last post by:
I am working on a report filter form. It has 2 combo boxes (cmboSelectSubject and cmboSelectCategory) to select criteria. Selecting an entry in combo 1 restricts the options available in combo 2....
1
by: Josh C. | last post by:
I am attempting to dynamically add an option to a select box when the box is selected (click or focus). This works fine using Firefox by either creating a new element and appending it to the...
5
tjc0ol
by: tjc0ol | last post by:
Hi all, I made contact page which allows visitors to input their name, email address, phone number, comments and select a comment type by using <select> element in html with javascript. Among the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.