最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - javascript variable implicitly declared - Stack Overflow

programmeradmin1浏览0评论

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 ? try var 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
Add a ment  | 

2 Answers 2

Reset to default 3

Use 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

发布评论

评论列表(0)

  1. 暂无评论