Solved

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

Posted on 2004-04-01
7
672 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML Video won't Autoplay on site 6 28
How to orderly load jquery libs with requireJS? 3 82
Where do I learn ES6? 3 70
Fulfillment API php code sample 1 41
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to count occurrences of each item in an array.

831 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