Solved

Cross-Browser Support For appendChild

Posted on 2006-11-28
6
936 Views
Last Modified: 2008-01-16
Experts, I am using the follow snipet of code as a test case comparing FireFox and IE. The following works in IE but does NOT work in FireFox. I'm not sure what I'm missing.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
    function AddStuff()
    {
        var MyNewElement = document.createElement("div");
        MyNewElement.innerText = "Hi";
        document.getElementById("AddStuffToMe").appendChild(MyNewElement);
    }
    </script>
   
</head>
<body>
<a href="javascript:AddStuff();">Add</a>
<div id="AddStuffToMe"></div>
</body>
</html>

Thanks for your help!

~ C
0
Comment
Question by:clickclickbang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Accepted Solution

by:
system_down earned 300 total points
ID: 18029019
hi try like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
    function AddStuff()
    {
        var MyNewElement = document.createElement("div");
        MyNewElement.innerHTML = "Hi";
        document.getElementById("AddStuffToMe").appendChild(MyNewElement);
    }
    </script>
   
</head>
<body>
<a href="javascript:AddStuff();">Add</a>
<div id="AddStuffToMe"></div>
</body>
</html>

innerText -> innerHTML
Let me know
S_D
0
 
LVL 6

Assisted Solution

by:Basilisci
Basilisci earned 150 total points
ID: 18029075
The "standard way" to insert text is using appenChild also, so instead of this:

MyNewElement.innerText = "Hi";
document.getElementById("AddStuffToMe").appendChild(MyNewElement);

You could (or should) do:

document.getElementById("AddStuffToMe").appendChild(document.createTextNode("Hi!"));

But innerHTML propably works also, it just is sometimes unreliable in what content it accepts, and you may find your content intended as plain text to be interpreted as HTML markup.
0
 
LVL 6

Expert Comment

by:system_down
ID: 18029281

innerText seems not to work on every Browser even if it should :D.

S_D

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 50 total points
ID: 18033470
innerText is not part of W3C, it's a Microsoft only extension

http://www.quirksmode.org/dom/w3c_old.html

0
 
LVL 1

Author Comment

by:clickclickbang
ID: 18069494
Thanks for all your help! Good tip on innerText :)
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 18069522
You're welcome.  Good luck
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question