Click here to Skip to main content
11,644,982 members (70,371 online)
Rate this: bad
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
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.
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_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.
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?
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.
Rate this: bad
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) {
  } else {
    g_selectedCheckboxId.splice(g_selectedCheckboxId.indexOf(, 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

Niral Soni
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
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;

Here I included "return false". Which avoids postback for a button click and your code will work fine...

Hope it works...

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

  Print Answers RSS
0 OriginalGriff 725
1 494
2 Sergey Alexandrovich Kryukov 464
3 Afzaal Ahmad Zeeshan 404
4 CPallini 386
0 OriginalGriff 1,020
1 Sergey Alexandrovich Kryukov 673
2 DamithSL 656
3 Afzaal Ahmad Zeeshan 652
4 CPallini 555

Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2015
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