First make your template into a jQuery object:
var template = $("<li><div class='bar'>bla</div></li>");
Then set the attributes and append it to the DOM.
template.find('li').attr('id','1234');
$(document.body).append(template);
Note that it however makes no sense at all to add a li directly to the DOM since li should always be children of ul or ol. Also it is better to not make jQuery parse raw HTML. Instead create a li, set its attributes. Create a div and set it's attributes. Insert the div into the li and then append the li to the DOM.