0

There's a HTML:

<div class="test">
<ul>
    <li>Item 1</li>
    <li>Item 2</li>
</ul>
</div>

And a bit of JS:

$(document).ready(function() {
    // do stuff when DOM is ready
    $(".test ul").append('<li>Foo</li>');
});

Why does it add nothing, but if I remove html from append's argument string,

.append('Foo')

it works - but that's not the point - new text is added as anonymous block, not li item as I wanted.

Anu suggestions?

edit: Argh, I've found the problem. I modified a file saved from Firefox's "Save page" option, the extension was .xhtml - and here's the problem. I renamed it and it works fine.

Thanks :)

3 Answers 3

1

adding actual HTML from JavaScript is rather ugly, have you tried something like this:

var li=document.createElement("li");
li.appendChild(document.createTextNode("Dupa"));
$(".text ul").appendChild(li);

?

Sign up to request clarification or add additional context in comments.

1 Comment

jquery does this behind the scenes
0

I checked your code and it works perfectly on my machine... So I think it's not jQuery bug.

Comments

0

Extending Frans-Willems code:

jQuery(function($){
   /* This is exactly the same as that document ready thing */
   var li=document.createElement("li");
   $(li).text("Dupa");
   $(".text ul").append(li);
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.