-1

This is where I'm at.

JavaScript:

var name = 'Bob Test';
document.getElementById('Bob Test').innerHTML = name;

HTML:

<p id="name"></p>
1
  • 2
    what exactly you want to do ? Commented Oct 23, 2018 at 17:39

3 Answers 3

2
var name = 'Bob Test';
document.getElementById('name').innerHTML = name;

You were using 'Bob Test' as the id in getElementById() when you should be using the id of the element you want to add to. In this case it is 'name'

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

7 Comments

and I would link my js file to my html with <script src="myJS.js"></script>
It depends on where your JS file is located, but yes you are correct, if the JS file is at the same level as the html file this will work.
Once linked, should I be able to use: <p id="name"></p> and it will work?
And if I can't get it to display anything?
Check your browser console to see if you have any errors in there. Its possible that you are not linking your JS file correctly too.
|
0

Instead of,

document.getElementById('Bob Test').innerHTML = name;

use

document.getElementById('name').innerHTML = name;

because your id is 'name'

2 Comments

So I would then be able to use those variables in html?
You can use <script> here type your js code </script> inside your html.
0

Class use as much as possible

<p class="name"></p>

my suggestion

var name = 'Bob Test';
document.getElementByClassName('name').innerHTML=name;

Because class can modify multiple element where. Id only selecting first element. You can use Queryselector.

For use of CSS selector. If you aware it just like

Jquery Selector.

And your current solution is

document.getElementId('name').innerHTML = name;

Comments

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.