Click here to Skip to main content
12,348,925 members (69,304 online)
Rate this:
 
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
   
What is the id? Why do you think it is defined?
Or defined it, but then there is no a question.
—SA
IpsitaMishra 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"
   
OK, so what's the problem? Use ID selector in jQuery.
—SA
IpsitaMishra 4-Feb-13 2:37am
   
Its giving all the selected Item but I want the last selected one.
   
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 4-Feb-13 4:35am
   
How to get the index ??? that is the Issue :P
   
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 5-Feb-13 2:18am
   
100 + for you Tadit
Tadit Dash 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
Top Experts
Last 24hrsThis month


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100