1

I found a weird behavior of jquery's html function.

I have the following code snippet.

HTML

<div id='content'></div>​

Javascript

var test2 = "<h5>test(S)</h5><span class='small_text>Apr 20, 2012 @ 07:00PM<br />Section 102 Row G Seat 14-14<br />";

$(document).ready(function(){

    $("#content").html(test2);

});​

When I run the code, it can only see 'test(S)'. It looks jquery ignores the rest of the string.

I created a jsfiddle.

http://jsfiddle.net/E3X33/

Am i using it incorrectly? or is there a undocumented stuff...?

3 Answers 3

5

You're not closing the <span> tag, plus closing quote of the class attribute

Should be:

var test2 = "<h5>test(S)</h5><span class='small_text'>Apr 20, 2012 @ 07:00PM<br />Section 102 Row G Seat 14-14<br /></span>";
Sign up to request clarification or add additional context in comments.

2 Comments

also the closing single quote in the class attr
The closing single quote is the bigger of the two problems. Most browsers can handle the non-closed span tag.
2

Yes and no. Your jQuery syntax is correct but your HTML is not.

Try this:

<h5>test(S)<\/h5><span class='small_text'>Apr 20, 2012 @ 07:00PM<br \/>Section 102 Row G Seat 14-14<br \/><\/span>

You needed to:

  1. Close your class attribute on the span tag.
  2. Close the span tag itself.
  3. Escape forward slashes for safety in bad browsers.

Comments

2

Try closing your span tag and closing ' for span class:

var test2 = "<h5>test(S)</h5><span class='small_text'>Apr 20, 2012 @ 07:00PM<br />Section 102 Row G Seat 14-14<br /></span>";

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.