Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setAttribute() problem in IE 2

Status
Not open for further replies.

dkdude

Programmer
Jun 16, 2003
849
DK
This code works fine in FF and NS. Shows nice red letters. In IE the style is not applied. Any ideas?

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
  <title>AppendStuff</title>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  <script type="text/javascript">
  <!--

  function newDIV() {
    var myEle = document.createElement('div');
    return myEle;
  }

  function addAttr(obj, prop, val) {
    obj.setAttribute(prop, val);
    return obj;
  }

  function addText(obj, text) {
    var myText = document.createTextNode(text);
    return addEle(obj, myText);
  }

  function addEle(container, content) {
    container.appendChild(content);
    return container;
  }

  function make() {
    var myBody = document.getElementsByTagName("body")[0];
    var myDiv = newDIV();
    myDiv = addAttr(myDiv, 'style','color:red');
    myDiv = addText(myDiv, 'DEMO');
    myBody = addEle(myBody, myDiv);
  }

  -->
  </script>
</head>
<body>
  <button onclick="make()">TEST</button>
</body>
</html>

Thanks
 
hi,

i'm pretty sure you need to use myDiv.style.cssText, not just myDiv.style.

my preference would be to have a css class already created, such as this:

Code:
.red { color: red; }

and then set the className attribute:

Code:
myDiv = addAttr(myDiv, 'className','red');



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks Cory - that worked like a charm ;-)

BTW - do you know of any browser plug-ins that can display the stuff generated dynamically? Sometimes it feels like working blindfolded when I're doing dynimics like this.

Thanks again - have a star!


Jakob
 
The firefox developers toobar has a "View Generated Source" option under the "view source" tab.


Additionally, you can save a javascript bookmarklet to view the derived code, adam wrote a faq on it (and many others) here:

faq216-4345

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
kath -thanks a bunch! The plug-in works wonders! And I just know it's gonna save me a lot of time too ;-)

Have a starrr!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top