0

I have a lot of checkboxes that needs to execute the same javascript function when checked/unchecked. As parameter I enclose the control's server ID. My hope is to get the actual checkbox using this ID. The problem is that I need to find the client ID, but as expected I can't use my javascript variable myControlsServerID inside asp code blocks to get this ID. This is where I get stuck:

function show(myControlsServerID) {
   var checkbox = document.getElementById('<%= myControlsServerID.ClientID %>')
   //more omitted code
}

How can I get the clicked checkbox?

3 Answers 3

1

Use jQuery to achieve this.

At document load initialize jQuery to listen to all checkboxes on your page. The moment you click on one of these, you can do whatever you want with it:

$(document).ready(function() {
    $("input:checkbox").click(function(e) {
        alert($(this).attr('id'));
    });
});

Download the latest jQuery version on http://jquery.com/download/

Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution! I don't even have to add the function to all checkboxes :) The only thing I still haven't figured out is how to get the "checked" status. It returns "undefined", while "type" and "id" show expected data. Will continue investigating. Thanks!
Try $(this).is(":checked") - this will return true or false
1
chk.attributes.add("onclick","show('" + chk.ClientID + "')");


function show(myControlsServerID) {
   var checkbox = document.getElementById(myControlsServerID);  
 }

1 Comment

Doing it this way takes a lot of extra code, because I have to add this attribute to every checkbox and I have many of them. But next time, when I have only a few of them, this will be the my solution :)
1

You can do like this: use onchange="show(this);"

No need of doing these <%= myControlsServerID.ClientID %> of stuffs, simply call show() on your <asp:checkbox onchange='show(this);'>, it will automatically give you that clicked/checked element or we can say checkbox.

<input type="checkbox" value="chk1" id="chk001" onchange="show(this);"/>
<input type="checkbox" value="chk2" id="chk002" onchange="show(this);"/>
<input type="checkbox" value="chk3" id="chk003" onchange="show(this);"/>
<input type="checkbox" value="chk4" id="chk004" onchange="show(this);"/>

Javascript

function show(element) {
    var checkbox = element.id;
    alert("You have clicked " + checkbox);
    //more omitted code
}

Fiddle Demo

1 Comment

I like this solution but your fiddle demo only shows how this works with the input control. I have noticed that an asp:checkbox renders as a span tag containing the checkbox. So "element" in your solution is in fact a span and I am unable to reach the checkbox anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.