/ Published in: jQuery
Dynamically add HTML tag to input description, matching the input by adding an ID value to the input.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$('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>