Solved

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

Posted on 2004-04-01
7
671 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 dynamically set the form action using jQuery.

911 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

20 Experts available now in Live!

Get 1:1 Help Now