41

Here is a very simple program and the output should be JavaScript but I am only getting s.

<html>
    <head>
        <title></title>
        <script type="text/javascript">
          document.getElementById("ma").innerHTML="JavaScript";
        </script>
    </head>
    <body>
        <h1 id="ma">s</h1>
    </body>
</html>

10 Answers 10

46

The element doesn't exist at the time you're attempting to set a value. You need to call this after the <h1> has been added to the DOM.

You can either move this <script> tag down further, or add your logic to a function which ought to be called when the document has been loaded:

window.onload = function() {
    /* Add your logic here */
}

Demo: http://jsfiddle.net/Lr2Hm/

Sign up to request clarification or add additional context in comments.

1 Comment

Also make sure there is no typo. .innerHTML works and not ` .innerHtml`.
15

You have to wait until the DOM has loaded.

window.onload = function(){
    document.getElementById("ma").innerHTML="JavaScript";
}

Comments

14

You have 2 choices:

window.onload = function() {
  //your script here
}

or

<body>
    <h1 id="ma">s</h1>
    <script type='text/javascript'>
        //your script here
    </script>
</body>

Comments

3

The options provided did not work for me. document.querySelector worked for me!

for example:

document.querySelector("#ma").innerHTML = "HELLO";

3 Comments

Thank you for this! This is what I needed, I have no idea why but this solution solved it for me.
why does this work and not document.querySelectorAll ?
This worked for me! Simple and efective.
1

Note that anything you write with element.innerHTML="anything" will replace the actual content of your element. If you want to add more content you have to store everything into a cumulative variable, the actual content of your element too, in order to keep it, then parse this variable into innerHTML when its content is full.

<script type=text/javascript">
var MyFullVar = element.innerHTML + "my new content I want to add";
element.innerHTML = MyFullVar;
</script>

Comments

1

You can also use the defer keyword with a script to execute it after the DOM has been initialized

<script type=text/javascript" defer>/*Your script here*/</script>

Comments

1

Just put your script tag at the bottom.

<!doctype html>
<html>
<head>
Head Content here...
</head>
<body>
Body Content...
<script>
script function here...
</script>
</body>
</html>

Comments

0

You could also do this instead...

<script type=text/javascript">
    window.onload = function () {
        var element = document.getElementById('element').innerHTML;
        var content = ".........";
        element.innerHTML += content.toString();
    }
    </script>

According to w3schools and basic JavaScript rules this works. I have also tried this before.

This takes the basis on the onload function to auto run the code at startup. It also takes advantage of the JS Assignment Operators to quickly and efficiently execute the code.

Comments

0

How about this

function change(){
  document.getElementById("content").innerHTML = "Javascript";
}
<html>
<div>
<p id="content">TypeScript</p>
<button onclick="change()">Change</button>
</div>
</html>

1 Comment

This doesn't seems to really answer to the question. Can you edit your answer to explain why this will fix the issue ?
0

This is what i have found works just link it in the html through the relative address then write the code in the js page

<body>     
 <h1 id="someText"></h1>
 <script src="link external javascript page here"></script>
</body>

in the script.js...

enter code here

//example

var age = prompt('What is your age?');

document.getElementById('someText').innerHTML = 'Hello World, I am ' + age + ' years old';

1 Comment

Hi @xAtom, welcome to Stack Overflow. This question has already great answers that are more to the point. You might want to just add a comment to an existing one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.