+ 4

Error: cannot set property 'onclick' on null

when I turn that Code by Netbeans ,it is working fine. whats is the problem in sololearn?

14th Mar 2017, 3:18 PM
ZeroOne;
ZeroOne; - avatar
7 Réponses
+ 7
change your code to this /* Show Password with Pure JavaScript */ function start(){ var myInput = document.getElementById('myInput'), myBtn = document.getElementById('myBtn'); myBtn.onclick = function () { 'use strict'; if (this.textContent === "Show Password") { myInput.setAttribute('type', 'text'); this.textContent = "Hide Password"; } else { myInput.setAttribute('type', 'password'); this.textContent = "Show Password"; } }; } window.onload=start; wrap it up in a function and call it once the HTML is fully loaded
14th Mar 2017, 3:35 PM
Burey
Burey - avatar
+ 6
/* Change Image Evert X Seconds */ function start(){ var myImge = document.getElementById('myImg'), arrImgs = [ "https://placehold.it/100/#fff", "https://placehold.it/100/#f00", "https://placehold.it/100/#00f", "https://placehold.it/100/#ddd" ]; function changeImg(myImge, arrImgs) { 'use strict'; setInterval(function () { var randomImg = Math.floor(Math.random() * arrImgs.length); myImge.src = arrImgs[randomImg]; }, 1000); }; changeImg(myImge, arrImgs); } window.onload = start; same problem, javascript attemp to access elements in the HTML before it is loaded completely so wrap everything with a function which will be called after HTML is loaded and call the function once inside
16th Mar 2017, 11:39 AM
Burey
Burey - avatar
+ 5
no probs :)
16th Mar 2017, 10:37 AM
Burey
Burey - avatar
+ 1
it's working..thanx alot Burey.
16th Mar 2017, 10:34 AM
ZeroOne;
ZeroOne; - avatar
+ 1
Buery..Could you please check error of :cannot set property 'src' of null..? what's the problem of 'src'
16th Mar 2017, 10:39 AM
ZeroOne;
ZeroOne; - avatar
+ 1
thank you Burey, i got the error where it came from..thanks 4 your help
16th Mar 2017, 12:06 PM
ZeroOne;
ZeroOne; - avatar
+ 1
window.onload=start();
25th Jun 2019, 1:26 AM
Abdullah Shaker
Abdullah Shaker - avatar