473,287 Members | 1,582 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,287 software developers and data experts.

Deselect the Value in ListBox (Urgent)

Hi,

I need an urgent help from you.

When we dynamically genrate any list box (Select Box) it shows one
default value as selected.

Is there any way that we can deselect that value.

I tried with document.form_Name.ListBox_Name.options[0].selected =
false;

I don't want any value to be selected in the list Box.

Its quite urgent.

Please help.

Thanks,
Deepa

Jan 4 '06 #1
7 13596
VK

de*********@rediffmail.com wrote:
Hi,

I need an urgent help from you.

When we dynamically genrate any list box (Select Box) it shows one
default value as selected.

Is there any way that we can deselect that value.

I tried with document.form_Name.ListBox_Name.options[0].selected =
false;

I don't want any value to be selected in the list Box.


Select list always have one option selected. It is also required to
have one option to be defaultSelected by using <option selected...

This is a model requirement.

For single choice w/o default value you have to use a set of radio
buttons:
<input type="radio" name="r01" value="1">
<input type="radio" name="r01" value="2">
...

For multiple choice w/o default value you have to use a set of
checkboxes:
<input type="checkbox" name="c01" value="1">
<input type="checkbox" name="c02" value="2">
...

Another option is to write your own pseudo-select element using DHTML
with any required properties. There is a lot of complications to
include such element into regular form element behavior paradigm but
it's doable. Of course it also means that your form will not be usable
w/o script support.

Jan 4 '06 #2
document.form_Name.ListBox_Name.selectedIndex = -1;

Jan 4 '06 #3
VK wrote:
de*********@rediffmail.com wrote:
Hi,

I need an urgent help from you.

When we dynamically genrate any list box (Select Box) it shows one
default value as selected.

Is there any way that we can deselect that value.

I tried with document.form_Name.ListBox_Name.options[0].selected =
false;

I don't want any value to be selected in the list Box.

Give the first option no value and no text and set it as the default
selected.
[...]

For single choice w/o default value you have to use a set of radio
buttons:


It is not certain that one of a set of radio buttons will not be
selected by default by the browser:

"If no radio button in a set sharing the same control name is
initially 'on', user agent behavior for choosing which control is
initially 'on' is undefined. Note. Since existing implementations
handle this case differently, the current specification differs
from RFC 1866 ( [RFC1866] section 8.1.2.4), which states:

At all times, exactly one of the radio buttons in a set is
checked. If none of the <INPUT> elements of a set of radio
buttons specifies `CHECKED', then the user agent must check the
first radio button of the set initially.

"Since user agent behavior differs, authors should ensure that in
each set of radio buttons that one is initially 'on'."

<URL:http://www.w3.org/TR/html4/interact/forms.html#radio>
Also, once one is selected it is impossible to get back to none
selected without using either a reset, page re-load or script.

[...]
--
Rob
Jan 4 '06 #4
VK

Lee wrote:
You should be able to generate a Select object with nothing selected,
but to deselect all:

document.form_Name.ListBox_Name.selectedIndex=-1;


Yes: this hack works for IE, Firefox and Opera - possibly it works for
all browsers. Sometimes the "law obeyant thinking" acts upon you when
you don't even feel that :-)

A select list with nothing selected still has something selected: it
will be the first option in the list. It will show up on the form
submission. So the hack must be applied to any select list: hardcoded
or dynamically created.

Jan 4 '06 #5
VK

Lee wrote:
From Netscape's Javascript documentation:

Options in a Select object are indexed in the order in
which they are defined, starting with an index of 0.
You can set the selectedIndex property at any time.
The display of the Select object updates immediately
when you set the selectedIndex property.

If no option is selected, selectedIndex has a value of -1.


So I see... Well, the first part is still ticking I guess as
document.forms[i].selectElement.selectedIndex = -1
works on all prowsers I was able to check. At the same time the second
part is not in action anymore. If no option(s) selected, the first
option will be selected by default (click submit and watch the address
bar):

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body>
<form method="GET" action="">

<select name="select1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

Jan 4 '06 #6
de*********@rediffmail.com wrote:
When we dynamically genrate any list box (Select Box) it shows one
default value as selected.
[...]
I don't want any value to be selected in the list Box.


At work, we always have a default first entry like "Please select from
the following". Or as someone else suggested, a blank first entry
(which you often see on commercial sites.) This is also helpful if
you need entry validation.

Luck!
Kev

Jan 4 '06 #7
Hey, Thanks a lot to all of you, this works fine.

document.form_Name.ListBox_Name.selectedIndex = -1;

Thanks once again. I appriciate the quick replies...

Deepa :-))

Jan 5 '06 #8

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

Similar topics

6
by: Orchid | last post by:
I have 2 fields with Radio button Data Type, and both fields with more than one options. On Field1, I want the user to select option1, then Field2 is shown and able to select. If the user selects...
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
1
by: Christian Dokman | last post by:
Hello, When I bind a datasource to a combobox, the first item is always selected, also when I set the SelectedValue property to -1 or 0. Also when I call the form's bindingcontext.AddNew, the...
4
by: GJP | last post by:
Hello. I have two list boxes. One with a picture name, and one for the ID of that picture (both coming from a database). The user selects the name of the picture and presses a button, the...
4
by: Bill Nguyen | last post by:
I would like to be able to do the following: Listbox1/Textbox1 contains list of all items not selected item A item B item D Listbox2//Textbox2 contains list of all items selected
1
by: dev.amit | last post by:
Hello friends Can anyone tell me how to deselect the item selected in the list box If Me!lb_test_property.ItemsSelected.Count > 0 Then For Each varItem In Me!lb_test_property.ItemsSelected...
3
by: Brian Simmons | last post by:
Hi All, I search on codeproject and google but didn't find what I was looking for. Anybody know of a good implementation where you have 2 listboxes and you want to move items between the 2. ...
1
by: Bailu | last post by:
Hi, I am a newbie in wxPython and doing a program with ListBox, I want to select and deselect items in this box, I have use self.devlist = wx.ListBox(self, style=wx.LB_MULTIPLE)...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...

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.