Solved

Cross-Browser Support For appendChild

Posted on 2006-11-28
6
916 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Timerpicker in javascript 4 34
MacOS and programming in React 7 27
jQuery detect if it is a mobile device 3 90
is this a cms? 8 32
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now