Add label for dynamically

[javascript]
$(‘ul li’).each(function(index) {
var spanContent = $(this).children(‘span’).html(),
spanContent = spanContent.toLowerCase().replace(/ /g, ‘-‘);
$(this).children(‘input’).attr(‘id’, ‘input-‘+spanContent);
$(this).children(‘span’).wrapInner(‘<label for="input-‘+spanContent+’"/>’);
});

<ul>
<li><input type="checkbox" name="input-name-1" value="" /><span>Description 1</span></li>
<li><input type="checkbox" name="input-name-2" value="" /><span>Description 2</span></li>
<li><input type="checkbox" name="input-name-3" value="" /><span>Description 3</span></li>
</ul>
[/javascript]

Leave a Reply