By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

selectedIndex value of the last selected item in select box

100+
P: 228
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
Share this Question
Share on Google+
6 Replies


P: 50
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
100+
P: 607
@samvb,
Can you please share the code what you have done?

Thanks and Regards
Ramanan Kalirajan
Mar 10 '10 #3

100+
P: 228
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
100+
P: 607
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

100+
P: 228
@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

P: 50
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

Post your reply

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