html - Why is my code working on jsFiddle but not locally -


As the title says, why this code works on my local HTML file, despite it it works on jsFiddle is. This is my code:

JS

  var s, t, d; S = document.getElementById ('Start'); T = document. GetElementById ('temporary'); D = document.getElementById ('display'); StartChange = function () {var v, nt, diff, timelapse, decrease, decrease, loop; V = + d.value; Nt = + t.value; Diff = v-nt; Timebank = 500; // Set whatever you want to lose = .2; Shirt = function () {var v = d.value; If (v> = (nt + decrement)) {loop = settimeout (function () {d.value = (v-decrease). Fix (1); deficiency ();}, timeliness)} and clear interval ( Loop); } Decrease (); } S.onclick = startChange;  

HTML

  & lt; Input id = "display" type = "text" value = "7.2" disabled & gt; & Lt; Input id = "temp" type = "text" & gt; & Lt; Button type = "button" id = "start" & gt; Set up the temple & lt; / Button & gt;    

You may have written your code to & lt; Head & gt; & Lt; / Head & gt; Your section of the file and your JavaScript code can not access the DOM elements because DOM is not created yet.

If your code is in the head, then it will:

  window.onload = function () {// your code}  

Otherwise it is recommended to include javascript before the closing body tag because the required content is first loaded.


Comments