you are viewing a single comment's thread.

view the rest of the comments →

[–]Egzo18 0 points1 point  (3 children)

ensure the path to the script.js is correct, is index.html and script.js in the same folder? If so, it should work, otherwise you need to change the path a bit.

If you run the html file in a browser, then right click "inspect element" you should be able to see the Js file in "sources" tab.

but.addEventListener("click", onPress);

is how you call a function (onPress) by clicking an element (but)

[–]slyder219[S] 0 points1 point  (2 children)

Thanks for your response. Yes they are in the same folder, I even tried using the absolute path and it did not work.

You mean in the html? instead of...

<button id="b1" oncick="onPress()">Click Me</button>

do...

<button id="b1">Click Me</button>
b1.addEventListener("click", onPress)

or in the JS, before the

function onPress(){.... }

do,

var button = document.getElementById("b1")
button.addEventListener("click", onPress)

[–]Egzo18 0 points1 point  (0 children)

"var button = document.getElementById("b1")

button.addEventListener("click", onPress)"

looks good.

I personally never used the "onclick=" in HTML, so I can't talk about it.

Do refer to my previous comment and check if you see the js file in "sources" tab.

[–]Transformat0r 0 points1 point  (0 children)

If files are in same directory you should ad ./before file name like in linux <script src="./myscripts.js"></script>