Rod Kite
Rod Kite

Reputation: 11

IE not displaying strings with HTML tags passed to innerHTML

I am passing a string containing HTML tags (center, ul, etc) to a function which then places it in a div, span or p innerHTML. This works great in all browsers but IE, which displays nothing. I tracked the problem the the tags, because plain text does display in IE.

I am using IE 9. The code is a little long for posting, but here is the idea:

str='<center>Some text</center>';
displayText('divId',str);
function displayText(id,str)
{ document.getElementById(id).innerHTML=str; }

Sorry everyone. I found that I could not set the innerHTML of a [P] element. I changed it to a [span] and it worked.

Upvotes: 1

Views: 816

Answers (2)

SBB
SBB

Reputation: 155

Glad that you found a solution..

You should consider using jquery. Then you don't have to worry about all the cross browser implementations & instead focus on adding/enhancing functionality & user experience on your site/application.

Upvotes: 0

gblazex
gblazex

Reputation: 50109

innerHTML is not a good idea (or needs a workaround) for <table>, <select>, <p> elements. This is a known limitation in older versions IE.

Also see this question IE innerHTML error.

Upvotes: 3

Related Questions