1

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

  • 4
    have you actually declared `acceptButton` ? try `var acceptButton = document.getElementById("accept");` – jtate Nov 06 '17 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)?*](https://stackoverflow.com/questions/1470488/what-is-the-purpose-of-the-var-keyword-and-when-to-use-it-or-omit-it) – RobG Nov 06 '17 at 22:41
  • Possible duplicate of [Declaring variables without var keyword](https://stackoverflow.com/questions/6888570/declaring-variables-without-var-keyword) – devlin carnate Nov 06 '17 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) –  Nov 06 '17 at 22:48

2 Answers2

3

Use the var keyword to declare your variable:

var acceptButton = document.getElementById("accept");
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
Peter Gardner
  • 320
  • 4
  • 9
1

It was because I loaded the javascript file at the top of the HTML before the DOM loads