473,410 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

selectedIndex value of the last selected item in select box

228 100+
I need to know how to get id of the last selected item in a multiple list box using javascript. It keeps on givin me 0 or 1...any tips would be great. Note i wanna store it in a variable and that variable should store id of the last selected item.
Mar 9 '10 #1
6 9372
I haven't done much with forms in JavaScript but I'd imagine you could do this with something like

Expand|Select|Wrap|Line Numbers
  1. var x = selected.item( selected.length - 1 ).id
  2.  
Mar 9 '10 #2
RamananKalirajan
608 512MB
@samvb,
Can you please share the code what you have done?

Thanks and Regards
Ramanan Kalirajan
Mar 10 '10 #3
samvb
228 100+
Expand|Select|Wrap|Line Numbers
  1. function SelectedItems(selectBoxName,maxallowed) {
  2.     //used to know how many items selected in <select box>
  3.  
  4.     var obj=document.getElementById(selectBoxName);
  5.     var lastclicked=obj.selectedIndex;
  6.  
  7.     var btnobj=document.getElementById(submitbutton);//submit or upload or so button.
  8.   var numberSelected=0
  9.  
  10.   for (var i=0; i < obj.options.length; i++) {
  11.      if (obj.options[i].selected == true){
  12.        numberSelected++;
  13.      }//end if
  14.         }//end loop
  15.  
  16.  
  17.  if (numberSelected>maxallowed){
  18.      alert("You can't select " + maxallowed + " items. Please deselect at least one item to proceed.");
  19.      btnobj.disabled=true;
  20.  
  21.      obj.options[lastIndex].selected=false;
  22.  return;
  23.  }//end if
  24.  else{
  25.  
  26.  }//end else
  27. }//end function
Now i wanted to store id of the last clicked item in lastindex variable.
Mar 10 '10 #4
RamananKalirajan
608 512MB
Hi samvb,
I was not able to understand you requirement clearly. I just gone through your code. From that I can understand that you are having a multiple select element. You can select so many options. Whether you want to find which option was selected finally?

Thanks and Regards
Ramanan Kalirajan
Mar 10 '10 #5
samvb
228 100+
@RamananKalirajan

When the user selects, say 3 which is the maximum allowed, i need to warn him about it and deselect the last item selected.
Mar 10 '10 #6
I misunderstood your post.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     var lastIndex = false;
  3.     function validate( select )
  4.     {
  5.         var max = 3;
  6.         var x;
  7.         var count = 0;
  8.         for( x = 0; x < select.options.length; x++ )
  9.         {
  10.             if( select.options[x].selected == true )
  11.                 count++;
  12.         }
  13.         if( count > max )
  14.         {
  15.             alert( "To many items selected!" );
  16.             select.options[lastIndex].selected = false;
  17.         }
  18.     }
  19. </script>
  20. <select id="myselect" onchange="validate( this );" multiple="multiple">
  21.     <option onmousedown="lastIndex = this.index">1</option>
  22.     <option onmousedown="lastIndex = this.index">2</option>
  23.     <option onmousedown="lastIndex = this.index">3</option>
  24.     <option onmousedown="lastIndex = this.index">4</option>
  25.     <option onmousedown="lastIndex = this.index">5</option>
  26.     <option onmousedown="lastIndex = this.index">6</option>
  27.     <option onmousedown="lastIndex = this.index">7</option>
  28. </select>
  29.  
Also do note we must use the onmousedown function not onclick or onmouseup onchange will happen before onclick and onmouseup in term making the function deslected the wrong option.
Mar 10 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Peder Y | last post by:
Anyone knows if there is some kind of property or function that will return the last selected/deselected item/index in a multiselect ListBox? SelectedIndex will point to first index in the...
1
by: jez123456 | last post by:
Hi, I have a windows form with a listbox control. My code all works correctly when deleting an item from the listbox except the last item. I get the following message when trying to delete the...
5
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value....
3
by: Stephen Adam | last post by:
Hi there, I'm sure i'm missing something really simple here, all i want to do is get the value of the selected item in a list box. Even after much fiddling about last night I still could not get...
2
by: Hei | last post by:
Hi, When a listbox SelectionMode set to MultiSimple, how can i know the last selected item? thx Hei
3
by: John Walker | last post by:
Hi, On an ASP.NET page I have a drop down list control. When the user pulls down the list and makes a selection, I perform validation, and if the validation fails I want the selected item in...
2
by: Midgard | last post by:
Hi there, I have this codes: <select name="my_items"> <option value="bmw">My Car</option> <option value="audi">My Old Car</option> </select>
3
lotus18
by: lotus18 | last post by:
Hello World It's me again. LOL, I know how to get the selected item on the second column of a listview. frmCourses.lvwCourses.SelectedItem().SubItems(2) But I don't know how to do it with...
3
pbala
by: pbala | last post by:
hi, I have the Listbox containing 250 items, when I selected 130th item and proceed with some operations. After that when i back to form, the selected items are same but i need to drag upto 130th...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.