473,503 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inserting an option into a select

I want to insert an option into a select.

I know you can do something like:
- Myselect.options[3] = new Option('Hello 3');
but that doesn't do what I want because it overwrites an existing option.

I have the existing options ordered on alphabet and the new option should
fit in on the right place. Of course I can move all the options that come
after the new one, but I wondered if there is a smarter way to do this.

Thanks,
Wim

Jul 20 '05 #1
2 7961

"Wim Roffal" <wi*************@nospamm-planet.nl> schreef in bericht
news:bs**********@reader08.wxs.nl...

I have the existing options ordered on alphabet and the new option should
fit in on the right place. Of course I can move all the options that come
after the new one, but I wondered if there is a smarter way to do this.


Try this:

<script type="text/javascript">
var selOptions = [];
function get_options (selObject) {
var tmp = [];
for (var i = 0; i < selObject.length; i++) {
tmp[tmp.length] = selObject[i].text;
}
return tmp;
}

function upd(selObject, v) {
selOptions = get_options(selObject.options);
selOptions[selOptions.length] = v;
selOptions.sort();
selObject.length = 0;
var selected = 0;
for (var i = 0; i < selOptions.length; i++) {
if (selOptions[i] == v) selected = i;
selObject[selObject.length] = new Option(selOptions[i]);
}
selObject.selectedIndex = selected;
}
</script>
....
<form>
<select name="s">
<option>a</option>
<option>b</option>
</select>
<br />
<input type="text" name="t" />
<input type="button" onclick="upd(s,t.value)" value="Update select"
/>
</form>
HTH,
JW

Jul 20 '05 #2


Wim Roffal wrote:
I want to insert an option into a select.

I know you can do something like:
- Myselect.options[3] = new Option('Hello 3');
but that doesn't do what I want because it overwrites an existing option.

I have the existing options ordered on alphabet and the new option should
fit in on the right place. Of course I can move all the options that come
after the new one, but I wondered if there is a smarter way to do this.


It depends on what browsers you want to support. Netscape 4, 6, 7,
Mozilla, IE 4, 5, 6, will allow you to set
select.options.length = select.options.length + 1;
then move some options and set
select.options[index] = new Option(...)

With newer browsers supporting the W3C DOM you should be able to simply
insert a new <option> element node into the select e.g. using insertBefore:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>inserting an option into a select with DOM Core methods</title>
<script type="text/javascript">
function insertOption (select) {
var newOption, optionToInsertBefore;
if (document.createElement && (newOption =
document.createElement('option'))) {
newOption.appendChild(document.createTextNode('opt ion created ' +
new Date()));
if (select.selectedIndex > -1) {
optionToInsertBefore = select.options[select.selectedIndex];
}
else {
optionToInsertBefore = null;
}
select.insertBefore(newOption, optionToInsertBefore);
}
}
</script>
</head>
<body>
<form action=""
onsubmit="return false;">
<p>
<select id="aSelect" size="5">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<input type="button" value="insert new option before selected option"
onclick="insertOption(this.form.aSelect);">
</p>
</form>
</body>
</html>

Tested with Netscape 7, IE6, and Opera 7, it should work with IE5 and
Netscape 6 as well.
If you look at the DOM HTML module at
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-94282980
then you will see that HTMLSelectElement objects should also have an add
method. That method should work with Netscape 6/7 and Opera 7 while IE5+
also has that method but with different arguments so you are better off
using insertBefore in my view.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #3

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

Similar topics

13
9267
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
1
3661
by: Geoff Wickens | last post by:
I am quite new to all this but am trying to create a database driven site. I have been able to use information from my sample database but I now want to be able to insert data into it. At present I...
14
4041
by: Miranda | last post by:
Hi, I have a ASP/vbscript program that generates random passwords. The problem is I need to insert those passwords into an Access database of 327 clients. I have the random password program...
0
2419
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
1
10776
by: Julia Briggs | last post by:
Hello, is there anyway to insert an image on any particular <select> option line in a drop down list without using a specialty drop-down? Any good ones out there that can be made to resemble a...
4
1427
by: comp_guy | last post by:
hey guys, can anyone see where im going wrong in my code below? i am making a registration form on a webpage before using php to send the input data into a mySQL database. first off is the code for...
1
1717
by: chumy | last post by:
I have a code that populates a select field with options and I need to insert into a database the value of the options rather than the id of the field. How can I accomplish this? Code: <?php ...
4
2553
by: sialater | last post by:
Hello, I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have...
4
2355
by: Joe Boy | last post by:
I am using a javascript to display a slide show of images. Each image is called with this in the body:- <select name="slide" onchange="change();"> <optionvalue="pictures/picture01.jpg"> Text...
0
7086
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
7280
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,...
1
6991
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
7462
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
5578
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,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.