Attaching events to checkboxes

I needed attach events to a group of checkboxes. So in my HTML page, I have a list of checkboxes, and a div tag to hold the results:

      <input id="1" type="checkbox" class="cb" /Do Stuff
      <input id="2" type="checkbox" class="cb" />Do More Stuff

I then have jQuery code to attach the actual events, and I call the attachCheckboxEvents function below when the page loads. Note that I am looping through the “cb” classes to get the DOM items I need:

      function attachCheckboxEvents() {
       $(".cb").click(function (e) {
           $("#results").html('<p>' + $(this).attr('id') + '</p>');
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: