2

If I have the following line in html:

<span></span> wtf

and then I run the jQuery statement:

$("span").html("test");

It ends up looking like "testwtf" instead of "test wtf". Is there anything I can do about this without changing the html?

B T
  • 57,525
  • 34
  • 189
  • 207
  • You might get better results on http://doctype.com/ – Bernhard Hofmann Oct 16 '09 at 08:03
  • 1
    $("span").html("test "); or $("span").html("test "); – Anand Shah Oct 16 '09 at 08:05
  • Thats not acceptable Anand. First of all, in other browsers, there would be an extra space. Second of all, it would then be impossible for me to omit a space in the HTML if I wanted to. This is about automating something. Clearly if I wanted to I could do it manually. – B T Oct 16 '09 at 08:14
  • don't browsers trim away multiple spaces? – Natrium Oct 16 '09 at 08:20
  • 2
    @BT - There would only be an extra space if the span is within a whitespace-preserving element (those are rare, I can only think of pre right now). Otherwise, multiple spaces are collapsed to one. So Anand's suggestion should work. – Tor Haugen Oct 16 '09 at 08:20
  • No, it won't work. If a nbsp is directly after the span, it'll improperly format it. – B T Oct 16 '09 at 08:45

1 Answers1

1

This happens because IE is interpreting the space in <span></span> wtf to be a leading space, and omits it (and interprets the HTML as <span></span>wtf). Because IE is interpreting the HTML differently than other browsers, I don't think there's much you can do without changing the HTML, except doing a workaround like

$CONTAINER.html('<span>test</span> wtf')

or an even more extreme workarounds such as

if (jQuery.browser.msie) $("span").html("test "); else $("span").html("test");

Edit: Preliminary testing shows that $("span").html("test "); by itself is also sufficient.

Zarel
  • 2,201
  • 1
  • 15
  • 20