admin管理员组文章数量:1431720
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
5 Answers
Reset to default 6The 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>
Don't know about a
write
property, butinnerHTML
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;
本文标签: javascriptCreating a website on the fly using getElementByIdStack Overflow
版权声明:本文标题:javascript - Creating a website on the fly using getElementById? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745425595a2658096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论