Friday, 16 August 2013

Text won't write to page but will show in alert() message. JavaScript and HTML

Text won't write to page but will show in alert() message. JavaScript and
HTML

I am developing a form that will gather some information. I have something
setup in my code where when the First Name text box changes (onchange),
the sub header (<td>) will then get populated with the First Name.
For some reason, the text never gets written into the <td> which had the
id of subHeader BUT when I do an alert message, it's there. What am I
doing wrong?
JavaScript (Doesn't work):
function FirstName_onchange() {
var LastName = document.getElementById('LastName').value;
var FirstName = document.getElementById('FirstName').value;
document.getElementById('subHeader').value = FirstName + " " + LastName;
}
JavaScript (Works):
function FirstName_onchange() {
var LastName = document.getElementById('LastName').value;
var FirstName = document.getElementById('FirstName').value;
alert(document.getElementById('subHeader').value = FirstName + " " +
LastName);
}
HTML:
<tr>
<td id="subHeader" colspan="4">
</td>
</tr>

No comments:

Post a Comment