views:

132

answers:

1

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" />');
   });
});
+4  A: 
// 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');
RaYell
Works perfectly, thanks.
Raúl Roa
Replacing 'li' for $("#your-ul").children('li') will allow adding the checkboxes to a specific unordered list.
Raúl Roa