javascript - JS not working with HTML (basic) -


to start, i'm bit of novice. i've been trying create js file contain text variables use in html file(among other functions). js file located in same directory html file (c:/websites/first). css file has linked , works properly, confusing me on why isn't working.

i've tried working answers these posts , few others, no luck:

set content of html <span> javascript

how change text of span element in javascript

how pass variable external javascript html form

what i'm trying here set

contain fullname var. once rest of problems should resolved too.

here's snippet of js:

    var fullname = "example";     document.getelementbyid('fullname').innerhtml = fullname; 

and snippet of html:

    <html>      <head>         <script type="text/javascript" src="script.js"></script>      </head>      <body>         <p id="fullname"></p>      </body>     </html> 

i can't find i'm doing wrong. i've been ripping hair out because simple haven't found solution.

put script after elements in dom, otherwise elements doesn't exist when you're trying them

<html>     <head>     </head>     <body>         <p id="fullname"></p>         <script type="text/javascript" src="script.js"></script>     </body> </html> 

another (not good) option waiting onload

window.onload = function() {     var fullname = "example";     document.getelementbyid('fullname').innerhtml = fullname; } 

or in modern browsers

document.addeventlistener("domcontentloaded", function(event) {     var fullname = "example";     document.getelementbyid('fullname').innerhtml = fullname; }); 

Comments

Popular posts from this blog

hibernate - How to load global settings frequently used in application in Java -

objective c - Ownership modifiers with manual reference counting -

python 3.x - Mapping specific letters onto a list of words -