Click here to Skip to main content
12,892,015 members (58,574 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
What is the id? Why do you think it is defined?
Or defined it, but then there is no a question.
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_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.
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?
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.
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 5-Feb-13 2:18am
100 + for you Tadit
Tadit Dash 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...
Member 12583703 16-Jul-16 0:28am
i have a checkboxlist with items,i placed a condition to select any number of items selected.I want to display the selected items as following in the label placed on webform.
if i select 1 item,should display as "option1".
if i select 2 items, should display as "option1 & option2."
if i select 3 items, should display "option1, option2 & option3." and so on
How could i add ampersand(&) in between the items selected.

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.170424.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2017
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