Raúl Roa
Raúl Roa

Reputation: 12386

Adding checkboxes to an unordered list using jQuery

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" />');
   });
});

Upvotes: 2

Views: 3177

Answers (1)

RaYell
RaYell

Reputation: 70414

// 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');

Upvotes: 6

Related Questions