6

If I have a span tag like this:

 <span>Hi<br />SecondLine</span>

And I call this jQuery function:

 var html = $('span').html();

html is set to:

 Hi<br>SecondLine

Now, I realize the name of the function is html() and not xhtml(). But is this expected behavior? Can I count on it, or do I alway need to check for a xhtml br tag and an html br tag, in say, this example:

function br2nl(text) {
    return text.replace(/<br \/>/gi, '\n').replace(/<br>/gi, '\n');
}
slolife
  • 19,520
  • 20
  • 78
  • 121

3 Answers3

9

It doesn't. It just modifies the browser DOM.

It is up to the browser to serialise the DOM to HTML or XHTML as it desires when using innerHTML. Different browsers act differently.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

All browsers normalize HTML code while building their DOM tree. So, javascript gains access to already normalized DOM. With all tags properly closed and such.

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
2

You aren't actually using XML, so it makes sense that the serialization doesn't use the XML syntax. See also Ian Hickson's essay about sending XHTML as HTML.

Ms2ger
  • 15,596
  • 6
  • 36
  • 35