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

Deselect the Value in ListBox (Urgent)

P: n/a
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
Share this Question
Share on Google+
7 Replies


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

P: n/a
document.form_Name.ListBox_Name.selectedIndex = -1;

Jan 4 '06 #3

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.