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
Solved

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

Posted on 2004-04-01
7
673 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with Connection / Parameter: 4 21
PHP parse error 7 61
AngularJS directive issue 6 72
cookies analysis tools 2 100
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…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

829 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