Forum Moderators: open
Now, the rub:
IE (any version -including 7-) will not allow this:
<p id="p_tag">To be Replaced</p>
<script type="text/javascript">
//<![CDATA[
document.getElementById('p_tag').innerHTML='Will work in IE'.
document.getElementById('p_tag').innerHTML='<div>Will not work in IE</div>'.
//]]>
</script>
In any (and I mean ANY) non-IE browser, you would see "Will not work in IE" displayed. However, on IE, you will see "Will work in IE".
This is IE Windows. IE Mac works properly (go figure).
Now, I've checked the W3C site, and can see no prohibitions on placing <div> tags inside of <p> tags.
However, I'm not particularly good at reading the darn specs, so there is every chance that I have missed it.
Does anyone have the skinny?
I'm pretty sure this is an IE bug, but IE is an 800-pound gorilla, and it won't be the first bug I've had to grin and bear.
If it is a bug, does anyone have a workaround?
p can never enclose a div in HTML (or XHTML served with the mime type text/html). If you are serving XHTML with an XML mime type, you can do this in theory, but the result would not be valid XHTML. When dealing with HTML or HTML-compatible XHTML, then the closing
</p> is optional, so the paragraph is always automatically closed when the parser encounters another block-level element.
The P element represents a paragraph. It cannot contain block-level elements (including P itself).
I can't see that a p-tag can be a replacement for a div-tag?
If you are serving XHTML with an XML mime type....
But if you are using true XHTML, you wouldn't be able to use innerHTML (would you?) - you would need to use the DOM methods... createElement(), appendChild() etc.
IE6 (and IE5 Win) actually report "Unknown runtime error" trying to execute that 2nd .innerHTML (not sure why?)
Have you tried putting <div> tags inside your p_tag to begin with?
<p id="p_tag"><div>To be Replaced</div></p>
To perform your original task of replacing the text with "<div>Sometext</div>" using the DOM, which I assume you would need for XHTML, then this works: (in FF, Opera AND IE6!)
var myp = document.getElementById('p_tag');
var newdiv = document.createElement('div');
var mytxt = document.createTextNode('This does work - but is it valid?!');
newdiv.appendChild(mytxt);
myp.replaceChild(newdiv,myp.firstChild); Although I question whether this is valid.
[w3.org...]
The P element represents a paragraph. It cannot contain block-level elements (including P itself).
I may have a bit of a workaround, but I'm not too happy with it.
You see, the only place I need to do this is in the administration interface. I don't want to hose up the entire site so some login-only stuff can get done. Practically speaking, I'm better off disallowing IE in the admin. interface than I am screwing up every single visitor to my site, or rewriting everything.
I'll try that "is it evil?" workaround first.
Thanks!
Div tags are not allowed in ANY block element tag, which includes <p>.
I figured that, but I wanted to find out where the standard says it, and why my pages pass every validator out there. There are a heck of a lot of HTML "urban legends" out there, and the obtuseness of the standard doesn't help clarify it.
Remember that XHTML 1.1 introduces a new "modular" structure to HTML, and that flexible block-level elements may well fit into this schema.
In the meantime, I'll explore alternatives, but I don't see many viable ones. I may switch <div> and <p> tags for the admin interface (using "generic" classes), but that seems like an awful lot of work for a one-time issue.
Div tags are not allowed in ANY block element tag, which includes <p>.
Incorrect--at least in html 4.01/xhtml 1.0.
The situation is much more nuanced, and many block level elements may contain div elements (including li, dd, td, th, div itself). To illustrate the point only for xhtml 1.0 strict, observe the following:
The situation seems similar in xhtml 1.1 [minerva.dce.harvard.edu], though I haven't looked deeply enough into it to be sure. There's still no particular need to use xhtml 1.1 for any but the most specialized needs as far as I can determine.
-b
In any case, I stand corrected. I need to figure out the best way to deal with this. I am considering removing the <div> enclosures for my admin content. I may not need them.
According to the standard, <p> can handle form elements, just not the form itself.