?
Solved

Cross-Browser Support For appendChild

Posted on 2006-11-28
6
Medium Priority
?
943 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 1200 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 600 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

752 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