Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: jQuery
I have one checkboxlist I want the Id of the checkbox that has been selected last in that checkboxlist.
Posted 3-Feb-13 20:10pm
Comments
Sergey Alexandrovich Kryukov at 4-Feb-13 2:17am
   
What is the id? Why do you think it is defined?
Or defined it, but then there is no a question.
—SA
IpsitaMishra at 4-Feb-13 2:28am
   
e.g the client id of my checkboxlist is ctl00_ContentPlaceHolder1_cblcheckbox
then under that i have 5-6 checkbox with client id
ctl00_ContentPlaceHolder1_cblcheckbox_0
ctl00_ContentPlaceHolder1_cblcheckbox_1
ctl00_ContentPlaceHolder1_cblcheckbox_2
ctl00_ContentPlaceHolder1_cblcheckbox_3 etc
 
I click the 3rd item of the checkboxlist then i want the id "ctl00_ContentPlaceHolder1_cblcheckbox_2"
Sergey Alexandrovich Kryukov at 4-Feb-13 2:32am
   
OK, so what's the problem? Use ID selector in jQuery.
—SA
IpsitaMishra at 4-Feb-13 2:37am
   
Its giving all the selected Item but I want the last selected one.
Sergey Alexandrovich Kryukov at 4-Feb-13 3:12am
   
All items? Well, you can select one by index. But if you use ID selector, it should give you only one. Will you show your code?
—SA
IpsitaMishra at 4-Feb-13 4:35am
   
How to get the index ??? that is the Issue :P
Sergey Alexandrovich Kryukov at 4-Feb-13 10:38am
   
Before you show your code and explain the purpose of it, it would be a waste of time.
I see no problems here, but don't even know what's on your input.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this out..
 
JavaScript part --
// Define a global variable array to store the last selected checkbox Id
var g_selectedCheckboxId = [];
 
// setter method to update checkbox id array
function setLastSelected(checkboxObj) {
  if(checkboxObj.checked) {
    g_selectedCheckboxId.push(checkboxObj.id);
  } else {
    g_selectedCheckboxId.splice(g_selectedCheckboxId.indexOf(checkboxObj.id), 1);
  }
}
 
// getter method to get ID of the last checked checkbox 
function getLastSelected() {
  if(g_selectedCheckboxId.length > 0)
    return g_selectedCheckboxId[g_selectedCheckboxId.length-1];
  return null;
}
 
HTML part (assumption) Add onclick event to each and every checkbox -
<input type="checkbox" id="ctl00_ContentPlaceHolder1_cblcheckbox_0" onclick="setLastSelected(this)">Checkbox 1
<input type="checkbox" id="ctl00_ContentPlaceHolder1_cblcheckbox_1" onclick="setLastSelected(this)">Checkbox 2
<input type="checkbox" id="ctl00_ContentPlaceHolder1_cblcheckbox_2" onclick="setLastSelected(this)">Checkbox 3
<input type="checkbox" id="ctl00_ContentPlaceHolder1_cblcheckbox_3" onclick="setLastSelected(this)">Checkbox 4
</br></br></br></br></script>
 
Regards,
Niral Soni
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
v3
Comments
IpsitaMishra at 5-Feb-13 2:18am
   
100 + for you Tadit
Tadit Dash at 5-Feb-13 2:23am
   
My pleasure. Thanks @Ipsita.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

May be because of Postback your code is not working.
 
Look at code below....
 
<script type="text/javascript">
    function show() {
    document.getElementById('light').style.display = 'block';
    document.getElementById('fade').style.display = 'block';
 
    return false;
     }
    </script>
 
Here I included "return false". Which avoids postback for a button click and your code will work fine...
 
Hope it works...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CHill60 360
1 OriginalGriff 203
2 Sarvesh Kumar Gupta 168
3 RyanDev 145
4 Peter Leow 145


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid