Solved

Cross-Browser Support For appendChild

Posted on 2006-11-28
6
924 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

856 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