Click here to Skip to main content
15,031,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to append the element(h1) in JS in a Div in HTML. I saw the tutorial and exactly copied it. It works fine if I show it in console. But it gives error when I try to append it in HTML.

In console it says " 'document.getElementsById' is not a function"

What I have tried:

HTML

<!doctype html>
<html>
    <head>
        <title>Javascript</title>
        <link rel="stylesheet" type="text/css" href="JS2.css">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta charset="utf-8">
    </head>
    <body>
        <div class="outer">
            <div class="text">Enter you age</div>
            <div class="inner"> 
                <button > click me</button>
                <button> Reset</button>
            </div>
            <div id="result">
            </div>
        </div>
        <script src="js2.js"></script>
    </body>
</html>


JavaScript

var element32=document.createElement('h1')
var name1 = document.createTextNode("Arslan");
element32.appendChild(name1);
console.log(element32);
document.getElementsById('result').appendChild(element32);
Posted
Updated 23-Jul-21 2:16am

1 solution

I think you mean to use getElementById[^]. The idea is there should only be one element in a page with a specific ID, so the method is singular rather than plural.
   
Comments
Arslan Anjum 2021 23-Jul-21 14:20pm
   
There is only one element with this ID. But in console it says 'document.getElementsById' is not a function
Chris Copeland 25-Jul-21 10:51am
   
This is because you're trying to use getElementsById and not getElementById. Remove the extra 's' after the 'Element' part.
Arslan Anjum 2021 27-Jul-21 6:31am
   
ooh, I see, Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900