2

What would be the jQuery code to add a checkbox to every <li> element in an unordered list and handle their callbacks obtaining the li text or checkbox properties like name/value?

I started with something like this:

$(document).ready(function() {
   $("#SuperTextbox1_Results").children('li').each(function() {
      $(this).prepend('<input type="checkbox" name="test" value="test" />');
   });
});
1
  • Please post the code for what you've tried and then I'm sure someone will help you out. Commented Aug 18, 2009 at 12:10

1 Answer 1

6
// checkbox click event handler
$('input:checkbox.liChk').live('click', function () {
    // access parent li, do whatever you want with it
    console.log($(this).parent('li'));
});    

// append checkboxes to all li tags
$('<input type="checkbox" class="liChk" />').appendTo('li');
Sign up to request clarification or add additional context in comments.

2 Comments

Replacing 'li' for $("#your-ul").children('li') will allow adding the checkboxes to a specific unordered list.
Since .live is deprecated in jQuery 1.7 and removed in 1.9, should probably use .on instead. (Simply replace .live with .on in example.) api.jquery.com/live

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.