Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Javascript Level 10 Lesson 3

 <!doctype html>
<html>
<head>
<title>Accessing HTML Element in JS</title>
</head>
<body>
<h1 id="heading">This is a heading</h1>
<p>Then comes a paragraph or a bunch of paragraphs.</p>
<p>So let's add another paragraph.</p>
<div class="parentDiv">
<div class="childDiv">Hello JS!</div>
<div class="childDiv">Hello JS again!</div>
</div>
<hr/>
<script>
document.write(document.getElementById("heading"));
document.write("<br/>"); // for line break
document.write(document.getElementByClassName("childDiv").length);
</script>
</body>
</html>


Error in length property
by

2 Answers

kshitijrana14
Try this one

<!doctype html>
<html>
<head>
<title>Accessing HTML Element in JS</title>
</head>
<body>
<h1 id="heading">This is a heading</h1>
<p>Then comes a paragraph or a bunch of paragraphs.</p>
<p>So let's add another paragraph.</p>
<div class="parentDiv">
<div class="childDiv">Hello JS!</div>
<div class="childDiv">Hello JS again!</div>
</div>
<hr/>
<script>
document.write(document.getElementById("heading"));
document.write("<br/>"); // for line break
document.write(document.getElementsByClassName("childDiv").length);
</script>
</body>
</html>
shubhamprkash
@iamabhishek
you have wrote
document.write(document.getElementByClassName("childDiv").length);

however the correct selector is .getElementsByClassName();
You missed the "s" in elements spelling.

Login / Signup to Answer the Question.