473,404 Members | 2,187 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.

How to add Item in SELECT listbox???

55
Hi !

Generating a dynamic HTML page which
gets a list of items in <SELECT> from db.
Onload of the html page i want to add text
"Select..." as the first item in the list.
How do i do it?

Code as follows:
[HTML]<select id='cities'>
<option value='1'>City1</option>
<option value='2'>City2</option>
<option value='3'>City3</option>
......
...
..
</Select>
[/HTML]
In the above code i need to add the text "Select..."
as first item.

Thanks in advance!
Hash
Nov 10 '07 #1
6 14009
acoder
16,027 Expert Mod 8TB
Use the add() method. Note that IE uses a non-standard method and the standard method, would you believe it, causes an error in IE. You will need to use a try...catch to deal with IE's bug.
Nov 10 '07 #2
hash4sp
55
I did used add() method. But it adds the item as the
last option. How to make it on the top of list?
Nov 10 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

have a look at the insertBefore() dom-method here

kind regards
Nov 10 '07 #4
hash4sp
55
hi acoder & gitts!

thanks for ur replies... earlier i didnt look carefully in the link given by
acoder... now it works fine...

my code as follows:

Expand|Select|Wrap|Line Numbers
  1. function AddItem(id){
  2.   var oOption = document.createElement("OPTION"); 
  3.   langext = parent.LanguageExtention;
  4.   if (langext=="A") 
  5.   {
  6.   txt = "...%u0627%u062E%u062A%u064A%u0627%u0631"//"...اختيار";
  7.   }
  8.   else 
  9.   {
  10.   txt = "Select...";
  11.   }
  12.   oOption.text=unescape(txt);
  13.   oOption.value=""; 
  14.   id.add(oOption,0); // where id is the object
  15.   id.options[0].selected=true;
  16. }
  17.  
Once again thanx a lot to u guys...

cheers!
Hash
Nov 10 '07 #5
acoder
16,027 Expert Mod 8TB
No that wouldn't work in non-IE browsers.

Test in, for example, Firefox. The correct method is to have an option object as the second parameter, not the selected index (which is what IE requires).

See this example.
Nov 11 '07 #6
Dasty
101 Expert 100+
hi ...

have a look at the insertBefore() dom-method here

kind regards
^^ exactly

Never use functions / methods that are not browser (standards) compatible. That will just result into unnecessary page restrictions. All reasonable browsers supports dom so:

[PHP]var newOption = document.createElement('option');
newOption.value = my_value;
newOption.appendChild(document.createTextNode('my_ text'));

// if you want to insert in the end
obj.appendChild(newOption);

// if you want to insert not in the end (on index position)
obj.insertBefore(newOption, obj.options[index]);
[/PHP]
(where obj is selectbox element)
gonna work widely.

Well there is one exception (but it's just a bug) that it's very rare. But in Opera when your selectbox is inside the hidden div and you want to alter it while it's hidden, "sometimes" opera acts strange. I wont describe it now, but remember: if you will have strange behavior in opera:
1) just disconnect selectbox from DOM tree (keep it in local variable for example)
2) make changes you need
3) attach it back on the same place to DOM tree
Nov 11 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mark | last post by:
How can I use the selections in a multi-select listbox as criteria in a query? Thanks! Mark
5
by: Melissa Cowan | last post by:
I am using Access 2000. I have the Developer's handbook and got the code for the mulit select listbox from there. It sends the selected value to another listbox, lstselected. What I need to do is...
1
by: | last post by:
Hello everybody... How can i unplug selected item in Listbox. Thanks in advance. Paulo
3
by: RockNRoll | last post by:
Greetings, Can I select an item in a listbox based on user textbox entry and a submit button? Can you provide any code examples for what needs to occur when the user clicks submit? Thank you,...
2
by: George Durzi | last post by:
This is more of a design/philosophical question :) I have a page in which the user sets attributes for a project. There are 8 attributes, e.g. Project Type, Country, etc. The number of options...
1
by: sab | last post by:
Hi, We have a web form with a listbox. The listbox is a multiple select listbox and has data that looks something like: ALL Unit 1 Unit 2 Unit 3 Note: "ALL" is always the first item in...
1
by: mkellum | last post by:
Using Access 2003, the selected item in listbox has backColor black and foreColor white. Ugly and hard to read. Any way to change these settings for ONLY the selected item using VB?
0
by: scartin | last post by:
Hi, I have a multi-select ListBox that shows 5 rows at a time, but contains possibly a hundred or more items. As I retrieve relevant data from the database, I set the SelectedIndex of the rows...
2
by: joyce85 | last post by:
hai i had 1 prblm of adding item to listbox.. but i succeeded that with the help of datasouce.. but i am not able to modify items in listbox..how can i do that my coding are
1
pbala
by: pbala | last post by:
I have a multiple select Listbox.When I click on it then it post backs and scroll back to the top of the Listbox. Is there any property to prevent it?
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...

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.