Solved

DHTML and JavaScript - writing text to a <DIV> from a variable

Posted on 2004-04-01
7
670 Views
Last Modified: 2010-04-06
is it possible to write text to a div with JavaScript?  i want to change adjust a number in the text based on the user input.  
0
Comment
Question by:dancing_penguin
  • 5
  • 2
7 Comments
 
LVL 17

Expert Comment

by:dorward
ID: 10730342
0
 
LVL 17

Expert Comment

by:dorward
ID: 10730370
<div id='foo'>0
</div>
<button onclick="document.getElementById('foo').replaceChild(document.createTextNode(i++),document.getElementById('foo').firstChild)">Hello</button>
<script type="text/javascript">
var i = 1;
</script>
0
 
LVL 17

Accepted Solution

by:
dorward earned 125 total points
ID: 10730377
And this works from a user input

<div id='foo'>0</div>
<button onclick="document.getElementById('foo').replaceChild(document.createTextNode(document.getElementById('userinput').value),document.getElementById('foo').firstChild)">Hello</button>
<input type="text" id="userinput">
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 17

Expert Comment

by:dorward
ID: 10730384
Or this.

<div id='foo'>0</div>
<input  onkeyup="document.getElementById('foo').replaceChild(document.createTextNode(this.value),document.getElementById('foo').firstChild)">
0
 
LVL 35

Expert Comment

by:YZlat
ID: 10731653
use innerHTML
0
 
LVL 35

Expert Comment

by:YZlat
ID: 10731687
<script language="javascript">
<!--
function writeText(text){
   if (document.all)
       document.all.mydiv.innerHTML = text;
}
//-->
</script>

<div id="mydiv">
test
</div>
0
 
LVL 17

Expert Comment

by:dorward
ID: 10731715
innerHTML is non-standard, and document.all is positively harmful.

Use document.getElementId where possible (as it is the public standard). If you need support for Internet Explorer 4.0 (which is the only browser to support document.all but not document.getElementById) then you can do something like

if (document.getElementById) {
  obj = document.getElementById('foo');
} else if (document.all) {
  obj = document.all.foo;
}

However, given the market share of IE 4.0, it really isn't worth it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to dynamically set the form action using jQuery.

743 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

11 Experts available now in Live!

Get 1:1 Help Now