javascript - Creating a website on the fly using getElementById? - Stack Overflow

I am trying to create a web site on-the-fly using JavaScript, but can't get my getElementById func

I am trying to create a web site on-the-fly using JavaScript, but can't get my getElementById function to work properly. I've poked around a good bit and found examples and try w/o success to make them work(the majority of them did not use an external .js file). Below is the code my JavaScript and HTML:

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
".dtd">
<html xmlns="">
<head>
    <title>Final Project</title>
<script type="text/javascript">
</script>
</head>
<body>
<script type="text/javascript" src="finalProj.js">
</script>
<form name = "myForm">
<h1 id="myHeader"></h1>

JavaScript:

var firstName = ("RicK");
var courseName = ("WEB 180");

function myHeading()
{
document.getElementById('header').write = (firstName + lastName);
} 

I am trying to create a web site on-the-fly using JavaScript, but can't get my getElementById function to work properly. I've poked around a good bit and found examples and try w/o success to make them work(the majority of them did not use an external .js file). Below is the code my JavaScript and HTML:

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
    <title>Final Project</title>
<script type="text/javascript">
</script>
</head>
<body>
<script type="text/javascript" src="finalProj.js">
</script>
<form name = "myForm">
<h1 id="myHeader"></h1>

JavaScript:

var firstName = ("RicK");
var courseName = ("WEB 180");

function myHeading()
{
document.getElementById('header').write = (firstName + lastName);
} 
Share Improve this question edited Feb 15, 2013 at 15:48 Bill the Lizard 406k212 gold badges574 silver badges892 bronze badges asked Dec 3, 2010 at 22:50 RickRick 311 bronze badge 1
  • Are you calling the myHeading function anywhere ? – Gabriele Petrioli Commented Dec 3, 2010 at 22:54
Add a ment  | 

5 Answers 5

Reset to default 6
  1. The JavaScript code that should have access to certain DOM elements, has to e after the elements in HTML, otherwise the elements are not generated in DOM yet (assuming the JavaScript code you wrote is in finalProj.js):

    <body>
       <form name = "myForm">
           <h1 id="myHeader"></h1>
       </from>
       <!-- can access myHeader now -->
       <script type="text/javascript" src="finalProj.js"></script>
    </body>
    
  2. Don't know about a write property, but innerHTML should do it:

    document.getElementById('myHeader').innerHTML = firstName + lastName;
    

    And of course you have to call myHeading() too!

document.getElementById('myheader').innerHTML = "(" + firstName + ' ' + lastName + ")";

?

The ID you specified and the ID of the header it looks like you are trying to select don't match. You have:

<h1 id="myHeader"></h1>

and

document.getElementById('header').write = (firstName + lastName);

You need to change that to:

document.getElementById('myHeader').write = (firstName + lastName);

try something like this:

var firstName = "RicK";
var courseName = "WEB 180";

function myHeading() {
    document.getElementById('header').innerHTML = firstName + " " + lastName;
} 

The property you want is innerHTML (and you can omit the parenthesis):

document.getElementById('myHeader').innerHTML = firstName + lastName;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745425596a4627161.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信