Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1332
  • Last Modified:

UNselecting an item from a list box

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
Pete2003
Asked:
Pete2003
  • 6
  • 4
1 Solution
 
justinbilligCommented:
document.formname.selectboxname.selectedIndex = -1;

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


0
 
Pete2003Author Commented:
ok but which event do I call the Deselect function on ?
0
 
justinbilligCommented:
it depends on when you want to unselect anything

give me a scenario
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
justinbilligCommented:
<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
 
Pete2003Author Commented:
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
 
justinbilligCommented:
thats alot of work, you'd probably be better off to use a button
0
 
Pete2003Author Commented:
it's just that it works like that in MFC apps ...
0
 
justinbilligCommented:
mfc?
0
 
dakydCommented:
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
 
Pete2003Author Commented:
MFC: The OLD :) C++ windows applications ..

I figured it out though ...

You hold down CTRL and click on the entry to unselect it :)
0
 
justinbilligCommented:
oh, you didnt want javascript code to do it?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now