Solved

UNselecting an item from a list box

Posted on 2004-08-30
11
1,314 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

762 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

20 Experts available now in Live!

Get 1:1 Help Now