Deselect the Value in ListBox (Urgent)

D

deepagulati

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
 
V

VK

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.
 
L

Lee

(e-mail address removed) said:
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;

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

document.form_Name.ListBox_Name.selectedIndex=-1;
 
R

RobG

VK said:
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.

[...]
 
V

VK

Lee said:
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.
 
L

Lee

VK said:
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 :)


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.
 
V

VK

Lee said:
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.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>
 
L

Lee

VK said:
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.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


Yes, I had already tested it. Note, however that the second part
is still completely valid. The difference is simply that the first
option is selected by default, so apparently the only way for no
option to be selected is to set selectedIndex=-1;
 
K

Kevin Darling

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
 
D

deepagulati

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 :))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top