backTangent backTangent - 1 year ago 64
jQuery Question

Passing Attributes to Append function in Jquery Not Working

I am trying to pass attributes to a jquery append function. It appends the html but does not append the attributes. There are no errors shown in the console either.
Html Code :

<li>First Item</li>
<li>Second Item</li>

Javascript Code :

$(this).append('<span></span>', {
text: 'some text',
class: 'someClass'

I want the HTML to look like

<li>First Item <span class="someClass">some text</span></li>
<li>Second Item Item <span class="someClass">some text</span></li>

Answer Source

.append() is a Method, not an element object


  $('<span />', {
    class : 'someClass', 
    text : ' some text',
    appendTo : this
<script src=""></script>
    <li>First Item</li>
    <li>Second Item</li>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download