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

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -