so im having an issue with some javascript and html.
in my html i have a button set up with an id
<td><button id = "accept">Accept</button></td>
and in the javascript onload function i have
acceptButton = document.getElementById("accept");
But for some reason it just started to say, variable implicitly declared , and when i try add anymore javascript, the button does not function. I am very new to javascript and really struggling to work out what this issue is due to, can someone maybe shed some light on it? thanks
I tried adding var, it takes away the error but stops the buttons functionality
so im having an issue with some javascript and html.
in my html i have a button set up with an id
<td><button id = "accept">Accept</button></td>
and in the javascript onload function i have
acceptButton = document.getElementById("accept");
But for some reason it just started to say, variable implicitly declared , and when i try add anymore javascript, the button does not function. I am very new to javascript and really struggling to work out what this issue is due to, can someone maybe shed some light on it? thanks
I tried adding var, it takes away the error but stops the buttons functionality
Share Improve this question edited Nov 6, 2017 at 23:21 asked Nov 6, 2017 at 22:38 user8700773user8700773 4-
4
have you actually declared
acceptButton
? tryvar acceptButton = document.getElementById("accept");
– jtate Commented Nov 6, 2017 at 22:39 - acceptButton isn't declared at all, so you should. See What is the purpose of the var keyword and when to use it (or omit it)? – RobG Commented Nov 6, 2017 at 22:41
- Possible duplicate of Declaring variables without var keyword – devlin carnate Commented Nov 6, 2017 at 22:44
- I need to use var? ive been taking javascript classes in university and when doing it the way i showed, we have never used var there :/ ? I just tried that, and it does take away the error im getting but it stops the button from working (button is set up to display a table) – user8700773 Commented Nov 6, 2017 at 22:48
2 Answers
Reset to default 3Use the var
keyword to declare your variable:
var acceptButton = document.getElementById("accept");
It was because I loaded the javascript file at the top of the HTML before the DOM loads