Solved

UNselecting an item from a list box

Posted on 2004-08-30
11
1,321 Views
Last Modified: 2012-06-27
Ok,

I have a diffrent question ... I have a list box, I can select one or many items in that list box ... now how do I UNselect an item or multiple items .. I have tried clicking on the listbox where there is no items and on the page off the controll .. no luck ...

any help


Txs
Peter
0
Comment
Question by:Pete2003
  • 6
  • 4
11 Comments
 
LVL 15

Expert Comment

by:justinbillig
ID: 11930474
document.formname.selectboxname.selectedIndex = -1;

function Deselect( objTargetSelectBox )
{
             objTargetSelectBox.selectedIndex = -1;
}


0
 

Author Comment

by:Pete2003
ID: 11930518
ok but which event do I call the Deselect function on ?
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 11930739
it depends on when you want to unselect anything

give me a scenario
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 15

Accepted Solution

by:
justinbillig earned 250 total points
ID: 11930795
<script language="javascript">

function Deselect( objTargetSelectBox )
{
             objTargetSelectBox.selectedIndex = -1;
}
      
</script>
<html>


      <body >
            <form name="frmTest">
                  <select name="cmbTest" multiple size="5">
                        <option> Option 1
                        <option> Option 2
                        <option> Option 3
                        <option> Option 4
                        <option> Option 5

            <input type="button" name="btnDeselect" value="Deselect" onclick="Deselect( frmTest.cmbTest );">
            </form>
      </body>
</html>
0
 

Author Comment

by:Pete2003
ID: 11931017
Ok sorry wasn't specific ...

I want it to unselect when  I click on the listbox where there is no data (empty part of listbox) or if I click anywhere else on the page that holds the listbox
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 11932138
thats alot of work, you'd probably be better off to use a button
0
 

Author Comment

by:Pete2003
ID: 11933539
it's just that it works like that in MFC apps ...
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 11933689
mfc?
0
 
LVL 19

Expert Comment

by:dakyd
ID: 11933729
How about something like this?  It writes onclick handlers to all of your selects, and will "unselect" the current option if you either click on it again or click somewhere inside the drop-down that doesn't have an option.  The only catch is that your selects MUST have id's, and they need to be unique.  That's how it keeps track of which drop-down has which option chosen at the moment, and multiple selects with the same id, or a select without an id would mess that up.  Regardless, hope this helps.

<!DOCTYPE PUBLIC HTML "-//W3C//DTD PUBLIC HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<script type="text/javascript">
window.onload = init;

function init()
{
  var sels = document.getElementsByTagName("select");
  lastSelected = new Array(sels.length);
  for (var i = 0; i < sels.length; i ++)
  {
    sels[i].onclick = deselBehavior;
    lastSelected[sels[i].id] = sels[i].selectedIndex;
  }
}

function deselBehavior()
{
  if (this.selectedIndex == lastSelected[this.id])
  {
    this.selectedIndex = -1;
    lastSelected[this.id] = -1;
  }
  else
  {
    lastSelected[this.id] = this.selectedIndex;
  }
}
</script>
</head>

<body>
<select id="theSel" size="10">
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
  <option value="d">d</option>
  <option value="e">e</option>
</select>
</body>
</html>
0
 

Author Comment

by:Pete2003
ID: 11939376
MFC: The OLD :) C++ windows applications ..

I figured it out though ...

You hold down CTRL and click on the entry to unselect it :)
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 11941634
oh, you didnt want javascript code to do it?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery 2 23
Responsive Font Size 6 30
Only allow one check box 17 27
How to change the colour of columns using dynamic data for google charts 3 9
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question