Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one checkboxlist I want the Id of the checkbox that has been selected last in that checkboxlist.
Posted
Comments
Sergey Alexandrovich Kryukov 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 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 4-Feb-13 2:32am    
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.
Sergey Alexandrovich Kryukov 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

try this out..

JavaScript part --
JavaScript
// 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 -
HTML
<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
 
Share this answer
 
v2
 
Share this answer
 
v3
Comments
IpsitaMishra 5-Feb-13 2:18am    
100 + for you Tadit
My pleasure. Thanks @Ipsita.
May be because of Postback your code is not working.

Look at code below....

XML
<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...
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900