Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Generate text using Javascript(document.text.innerHTML)

Posted on 2004-04-01
7
Medium Priority
?
294 Views
Last Modified: 2007-12-19
hi,
i'm suppose to generate text in a normal html page using .innerHTML.

my source code is as follows:

<html>
<body>
<p id="text" />

<script>
   p1.InnerHtml = "<b>You wrote:</b> "
</script>


</body>
</html>

---------------------------------------
however no text appeared. What's wrong with the code?
Btw, is there such a function called "document.text.innerHTML"?

Please advise. Thanks~
0
Comment
Question by:Alasse
[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
  • 4
  • 2
7 Comments
 
LVL 17

Expert Comment

by:mreuring
ID: 10739036
There's no such object as p1, you could acces it (in this html-block) as document.getElementsByTagName("p")[0].InnerHtml
Seeying as it's empty, a more standards aware choice would be:
document.getElementsByTagName("p")[0].appendChild(document.createTextNode("<b>You wrote:</b>"));

Or the best solution in your html-fragment:
document.getElementById("text").appendChild(document.createTextNode("<b>You wrote:</b>"));
0
 

Author Comment

by:Alasse
ID: 10739078
okay, i did this instead:

<html>
<head>
<title>Kids Furniture.com - About Us</title>
<link href = "templates/forText.css" rel="stylesheet" type="text/css">
</head>

<body onload="init_text();" >

<table>

<tr>
<td>

<?
      include("includes/top_bar.php");
?>
</td>
</tr>


<tr><td>
<font>
<p id="content" >

</p>
</font>
</td></tr>


</table>

<script>
function init_text() {
      document.all('content').innerHTML="This is a default setup of the KidsFurniture.Com project, products shown are for demonstrational purposes, any products purchased will not be delivered nor will the customer be billed. Any information seen on these products is to be treated as fictional."

}

</script>
</body>
</html>

now i'm suppose to run it on external javascript. How do i go about doing it?
0
 
LVL 17

Expert Comment

by:mreuring
ID: 10739134
Place the script in a file, without the <script> tags (Just making sure) and link to it like this:
<head>
<script type="text/javascript" src="yourjavascript.js"></script>
</head>

And I want to stress the point that using document.all is IE only, it seems a shame to restrict yourself to that when there's a couple of standards-compliant ways to do this with.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:Alasse
ID: 10739146
i manage to do that.... i mean the external script part...

you mention document.all is not excatly the perfect solution, restrictions and all.

So what's the other solutions that's better? Please tell me about it.

I duno. My lecturer told me to use this way. Then again, if there's other better ones.... =)

Please advise again. Thank you!
0
 
LVL 17

Expert Comment

by:mreuring
ID: 10739186
The better way would be using:
document.getElementById('content')

This would work in IE4+ IE5+ and IE6+ as well as Konqueror, Gecko-based browsers (Mozilla, Netscape) and Opera.

Good luck and my pleasure,

 Martin
0
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 10739866
as far as i know, innerHTML is an IE only command. better to use innerText like document.getElementById("content").innerText and for an ultimate crossbrowser, why not document.write()?

gam3r
0
 
LVL 17

Accepted Solution

by:
mreuring earned 150 total points
ID: 10739956
Uhm, well, not document.write because that runs while loading the page not after loading it and is as such not dynamic at all. ".innerHTML" is an IE invention but supported by most browsers since then:
http://www.quirksmode.org/dom/innerhtml.html

If you want to go for compliancy use the DOM by either creating a textNode of modifying one if already present:
document.getElementById("content").firstChild.nodeValue = "new text"; //Already exsisting textNode
document.getElementById("content").appendChild(document.createTextNode("new text")); //create a new node

Martin
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

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?
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

722 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