When trying to invoke a JavaScript function of a .js file from .html file the function doesn't being invoked.
Project hierarchy:
- html (folder)
- main.html
- js (folder)
- script.js
Example #1 - using .html - Works
main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Main page</title>
</head>
<body>
<label id="label1"></label>
<input id="text1" type="text"/>
<button onclick="document.getElementById('label1').innerHTML = document.getElementById('text1').value">Button</button>
</body>
</html>
Example #2 - using .html and .js - Doesn't work
main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Main page</title>
</head>
<body>
<label id="label1"></label>
<input id="text1" type="text"/>
<button onclick="outputInput()">Button</button>
<script src="/js/script.js"></script>
</body>
</html>
script.js
function outputInput(){
document.getElementById('label1').innerHTML = document.getElementById('text1').value;}
Failed to load resource: the server responded with a status of 404 (Not Found). I am using Chrome.srcattribute is the culprit. Try "../js/script.js"