Solved

UNselecting an item from a list box

Posted on 2004-08-30
11
1,316 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now