Solved

Cross-Browser Support For appendChild

Posted on 2006-11-28
6
911 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now