I have included code which works perfectly but when I move the javascript tags and code to a external .js file the function call in the html file is not recognized "function is not defined". I believe the problem is scope, of the function call in the html file... apparently its not global and therefore it is not recognized by the js file. When I moved the javascript code to an external file within the same folder as the html file I inserted <script src="myjsfile.js"></script> into the body section...I also tried inserting it into the head section, however it still didn't work. How can I make function calls (by button clicks) within my html file without having to include the javascript code in the html file? How would you fix this simple file?
<!DOCTYPE html> <html> <head> <title>My Title</title> <style> #myDIV { width: 200px; height:100px; background-color: #FC20FF; color: #27FFF8; } </style> </head> <body> <p>Click the button to set the backgroundColor property.</p> <button onclick="myFunction()">Execute myFunction</button> <div id="myDIV"> <h1>How are you?</h1> </div> <script> function myFunction() { document.getElementById("myDIV").style.backgroundColor = "#291CE8"; } </script> </body> </html>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)