Solved

UNselecting an item from a list box

Posted on 2004-08-30
11
1,330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

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!

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

628 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