[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

getElementById.name is undefined in Firefox but works in IE

I'm trying to figure out how to make this code work in in Firefox but Firefox gives undefined result.

Here's the code:

<html>
<title>tester</title>
<head>
<script>
function tester() {
var works = document.getElementById("test").name
document.write(works)
}
</script>
</head>
<body onload=tester();>
<div id=test name=who>
</div>
</body>
</html>
0
deek22
Asked:
deek22
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
Check this:

<script>
function tester() {
var works = document.getElementById("test").getAttribute("name");
document.write(works)
}
</script>

And document.write() does OVERWRITE! the page.
After document.write() there is NO div with the id "test".
Or you do it like this:
<html>
<title>tester</title>
<head>
<script>
function tester() {
var works = document.getElementById("test").getAttribute("name");
document.write(works)
}
</script>
</head>
<body onload="//tester();">
<div id=test name=who>
</div>
<div>
<script>
tester();
</script>
</div>
</body>
</html>

0
 
deek22Author Commented:
That works perfectly fine Zvonko.   Thanks soooo much....
0
 
ZvonkoSystems architectCommented:
You are welcome.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now