<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.
-
1What you have done is correct... What is your exact question?Netwidz– Netwidz2013年08月04日 00:38:54 +00:00Commented Aug 4, 2013 at 0:38
3 Answers 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>
Comments
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
Basically you executing the javascript before creating the html
<small id="name"></small>
so the javascript does not work on the non-existent html .