The preferred way of creating a new element with jQuery

The first option gives you more flexibilty: var $div = $(“<div>”, {id: “foo”, “class”: “a”}); $div.click(function(){ /* … */ }); $(“#box”).append($div); And of course .html(‘*’) overrides the content while .append(‘*’) doesn’t, but I guess, this wasn’t your question. Another good practice is prefixing your jQuery variables with $: Is there any specific reason behind using … Read more

How to add a list item to an existing unordered list

This would do it: $(“#header ul”).append(‘<li><a href=”https://stackoverflow.com/user/messages”><span class=”tab”>Message Center</span></a></li>’); Two things: You can just append the <li> to the <ul> itself. You need to use the opposite type of quotes than what you’re using in your HTML. So since you’re using double quotes in your attributes, surround the code with single quotes.

tech