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

x
?
Solved

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

Posted on 2004-04-01
7
Medium Priority
?
678 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
[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
  • 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 500 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
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.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

715 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