2
<script>
var cool = "cool";
var pass = document.getElementById('name');
pass.innerHTML = cool;
</script>
<small id="name"></small>

To be honest I am just not searching correctly; this seems very basic. I have been using jquery for everything, but never actually used much JS by itself, so I am wondering how one would append a value to an element using just JS.

1
  • 1
    What you have done is correct... What is your exact question? Commented Aug 4, 2013 at 0:38

3 Answers 3

3

Your code should work, but you need to add it after the element is defined, otherwise document.getElementById('name') will return null as name element doesn't exist yet.

<small id="name"></small>
<script>
var cool = "cool";
var pass = document.getElementById('name');
pass.innerHTML = cool;
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Your code should work... you have to define the JS after

<small id="name"></small>

If not there might be a problem which try to search DOM element before it loads

1 Comment

yes.. what is wrong in the answer?? is that means answer is wrong?
0

Basically you executing the javascript before creating the html

<small id="name"></small>

so the javascript does not work on the non-existent html .

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.