javascript - Uncaught TypeError: Cannot set property 'onfocus' of null -
i trying learn javascript , i'm building basic tutorial. in trying demonstrate onfocus , onblur, error message in javascript console: uncaught typeerror: cannot set property 'onfocus' of null.
here code. new learning javascript , use help.
//alert("hello, world!"); // javascript alert button // var year = 2014; var useremail = ""; var todaysdate = ""; /*var donation = 20; if (donation < 20) { alert("for $20 cookie. change donation?"); } else { alert("thank you!"); } */ var mainfile = document.getelementbyid("maintitle"); console.log("this element of type: ", maintitle.nodetype); console.log("the inner html ", maintitle.innerhtml); console.log("child nodes: ", maintitle.childnodes.length); var mylinks = document.getelementsbytagname("a"); console.log("links: ", mylinks.length); var mylistelements = document.getelementsbytagname("li"); console.log("list elements: ", mylistelements.length); var myfirstlist = document.getelementbyid("2 paragraphs"); /* can use: var limitedlist = myfirstlist.getelementsbytagname("li"); dig deeper dom */ var myelement = document.createelement("li"); var mynewelement = document.createelement("li"); //mynewelement.appendchild(mynewelement); var mytext = document.createtextnode("new list item"); mynewelement.appendchild(mytext); // creating elements var newlistitem = document.createelement("li"); var newpara = document.createelement("p"); // add content, either use inner html // or create child nodes manually so: // newpara.innerhtml = "blah blah blah..."; var paratext = document.createtextnode("and beginner level intro javascript! yay!"); newpara.appendchild(paratext); //and still need attach them document document.getelementbyid("basic").appendchild(newpara); var mynewelement = document.createelement("li"); var seconditem = myelement.getelementsbytagname("li")[1]; myelement.insertbefore(mynewelement, seconditem); // example of using anonymous function: onclick. //when click anywhere on page, alert appears. //document.onclick = function() { // alert("you clicked somewhere in document"); //} // , example of restricting click alert // element on page. var myimage = document.getelementbyid("mainimage"); myimage.onclick = function() { alert("you clicked on picture!"); } function prepareeventhandlers() { var myimage = document.getelementbyid("mainimage"); myimage.onclick = function() { alert("you clicked on picture!"); } //onfocus , onblur event handler illustration var emailfield = document.getelementbyid("email"); emailfield.onfocus = function() { if (emailfield.value == "your email") { emailfield.value = ""; } }; emailfield.onblur = function() { if (emailfield.value == "") { emailfield.value = "your email"; } }; } window.onload = function() { // preps , ensures // other js functions don't // called before document has // loaded. prepareeventhandlers(); // named function call nested inside anonymous function. } //sometimes want js run later or call // function in 60 seconds or every 5 sec, etc. // 2 main methods timers: settimeout , setinterval // these timer functions in milliseconds var myimage = document.getelementbyid("mainimage"); var imagearray = ["images/blue-roses.jpg", "images/purple-rose.jpg", "images/white-rose.jpg", "images/orange-rose.jpg", "images/pink-roses.jpg", "images/red-roses.jpg", "images/yellow-roses.jpg", "images/murdock.jpg", "images/dorothy-red-ruby-slippers.jpg"]; var imageindex = 0; function changeimage(){ myimage.setattribute("src",imagearray[imageindex]); imageindex++; if (imageindex >= imagearray.length) { imageindex = 0; } } setinterval(changeimage, 5000); //sometimes may want random alert // pop x-number of seconds later. //so use settimeout, so: /*function simplemessage() { alert("get ready learn!"); } settimeout(simplemessage, 5000); */ /*var_dump($_post); if var_dump($_post) = ""; return var($_get); error_log($_post); */
if it's giving error, means document.getelementbyid("email")
evaluates null
, means no element exists id email
.
that's can tell without seeing html js connected to.
Comments
Post a Comment