views:

390

answers:

3

I'm trying to do :

document.getElementById("header-text").innerHTML = "something interesting";

It's working fine with every browser except Opera (I'm running the latest version).

Browsing the internet I found people saying that Opera does not support innerHTML. That doesn't sound right.

Any pointers?

Thanks

Edit: Here is the id I'm trying to get

<div id="header-text" class="picture-text">
          <!--[if gt IE 5]>ugly hack<![endif]-->
          Some text generated server side
         <!--[if gt IE 5]>ugly hack<![endif]-->
</div>

Edit2: I have the same issue with :

<div id="header-text" class="picture-text">
          Static text
</div>

Edit3:

<body onload="intialize();">


function intialize() {
    operaHeaderFix();
}


function operaHeaderFix(){
    if(window.opera) {
        /*opera specific action*/ 
        document.getElementById("picture-line-wrapper").style.position="relative";
        document.getElementById("picture-line-wrapper").style.top="0px";
        document.getElementById("picture-line-wrapper").style.marginTop="-230px";
        document.getElementById("picture-line").style.padding="1px";
        document.getElementById("header-text").innerHTML = "<div style='margin:220px 0px 0px 20px;'>"+document.getElementById("header-text").innerHTML+"TEST</div>";
    }
}

Edit4: If I remove the if(window.opera), it will run fine in FF and IE

+1  A: 

FOUND THE ISSUE:

The wrapping node was not properly closed.

<div att1="value" att2="value"
  <div id="header-text">...</div>
</div>

AFAIK, FF and IE were politely fixing the tag... but Opera wasn't, thus the child DIV wasn't really an element in the DOM.


Should work fine. What is the element with the id "header-text"? Are you sure there is only one match, and it has editable content?

Can you post some of the markup so we can test?

From PPK's site, there doesn't seem to be any Opera issues: http://www.quirksmode.org/dom/innerhtml.html

Update: Running this code in Opera 9.6.4 on XP works fine for me.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title>innerHTML test</title>
<script>
  function doIt(){
    document.getElementById("header-text").innerHTML = "something interesting";
  }
</script>
</head>
<body>
  <div id="header-text" class="picture-text">
    Static text
  </div>
  <input type="button" value="Change It" onclick="doIt();"/><br/>
</body>
</html>
scunliffe
This is the only match. There are conditional comments (<!--[if gt IE 5]> etc) in it thought, could that be a problem?
marcgg
edited the question to include the div
marcgg
this is really weird... Updated the question to include how I call the function. Thanks for your help
marcgg
that was some great debugging ! thanks again. I feel bad for leaving such a terrible mistake in the code and not even thinking of looking for it !
marcgg
no worries it happens to us all. I too was so focused on that 1 element that I didn't think much about its context.
scunliffe
A: 

Edit: How are you calling this function? Are you calling it in the document <head>, at the end of the <body>, are you using window.onload?

innerHTML is not a w3c-valid attribute (it will be in HTML 5). It was created by Microsoft and is just supported by most other browsers for compatibility reasons.

That said, I believe Opera does support innerHTML, however some googling suggests that it is broken in certain versions. Try updating your version of Opera and see if it works then.

tj111
Like I said, I'm using the latest version of Opera
marcgg
(I saw this forum post while googling and updated ^^ Didn't fix anything though)
marcgg
updated my questions on how I call the function
marcgg