views:

105

answers:

2

In IE 8, jQuery acts as I would expect:

>>$('div',$('<a><div></div></a>')).html('test').html()
"test"

In FireFox:

>>> $('div',$('<a><div></div></a>')).html('test').html()
"<a>test</a>"

It puts anchors around what I wanted. Does anyone know why this would happen?

EDIT: Setting this with plain javascript (i.e. setting innerHTML) causes the problem. So I guess my real question is: why does firefox change what I set? Is this part of some esoteric specification, or is it a bug?

+4  A: 

Wrapping an <a> around a <div> is invalid html. Maybe Firefox is fixing it for you on the fly and returning the valid html?

hookedonwinter
Maybe, but it accepts divs inside a's in static html just fine. I guess it's not impossible that it has extra fixing stuff for dynamic html, but it seems weird.
Xodarap
I don't mean to say that firefox will edit your html. More that it might interpret the HTML in the DOM differently than the static html suggests.
hookedonwinter
Even though it might work fine in some browsers (IE, surprisingly) - I recommend you try to make it valid HTML. It'll make your life easier when it comes to debugging and validation enforcement.
MunkiPhD
I asked on jQuery and they said basically this - block inside inline isn't expected to work, so the result of setting innerHTML like this is undefined. So marking this as the answer, although Reigel posted a good workaround.
Xodarap
+2  A: 

well, firefox maybe knows you're breaking the rules.

but it did not know you did if you use .append().

$('div',$('<a><div></div></a>')).html('').append('test').html(); // give you 'test'
Reigel