Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'value' of null

Tags:

javascript

I'm getting error in this code, I'm trying to do an event where in when the page is load, it will do the event. But the problem is when I go to other function, but same page, it gets a error of null on that variable. It has no problem when I execute this codes, but when I'm on other part of my codes this error occurs.

Uncaught TypeError: Cannot read property 'value' of null

$(document).ready(function(){         var str = document.getElementById("cal_preview").value;       var str1 = document.getElementById("year").value;       var str2 = document.getElementById("holiday").value;       var str3 = document.getElementById("cal_option").value;           if (str=="" && str1=="" && str2=="" && str3=="" )           {             document.getElementById("calendar_preview").innerHTML="";               return;             }            if (window.XMLHttpRequest)           {// code for IE7+, Firefox, Chrome, Opera, Safari             xmlhttp=new XMLHttpRequest();           }           else           {// code for IE6, IE5             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");           }            xmlhttp.onreadystatechange=function()           {             if (xmlhttp.readyState==4 && xmlhttp.status==200)               {                 document.getElementById("calendar_preview").innerHTML=xmlhttp.responseText;               }           }          var url = calendar_preview_vars.plugin_url + "?id=" + str +"&"+"y="+str1+"&"+"h="+str2+"&"+"opt="+str3;         xmlhttp.open("GET",url,true);         xmlhttp.send();     }); 
like image 432
user2901740 Avatar asked Feb 27 '14 02:02

user2901740


People also ask

How do you fix TypeError Cannot read properties of null?

The "Cannot read property 'click' of null" error occurs when trying to call the click method on a null value. To solve the error, run the JS script after the DOM elements are available and make sure you only call the method on valid DOM elements.

What is TypeError Cannot read property of null?

While coding in Javascript, you must have at least once received a Type Error which has the message “cannot read properties of null”. This common error occurs when one tries to read a property or call a method on a null object. For Safari users, this same error comes like “TypeError: null is not an object”.

Can not set property of null?

To solve the "Cannot set property of null" error, make sure that the DOM element you are accessing exists. The error is often thrown when trying to set a property after using the getElementById() method and passing it a non-existent id. Copied! const el = document.

Can't access property value document getElementById is null?

This error TypeError: document. getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.


2 Answers

I am unsure which of them is wrong because you did not provide your HTML, but one of these does not exist:

var str = document.getElementById("cal_preview").value; var str1 = document.getElementById("year").value; var str2 = document.getElementById("holiday").value; var str3 = document.getElementById("cal_option").value; 

There is either no element with the id cal_preview, year, holiday, cal_option, or some combination.

Therefore, JavaScript is unable to read the value of something that does not exist.

EDIT:

If you want to check that the element exists first, you could use an if statement for each:

var str, element = document.getElementById('cal_preview'); if (element != null) {     str = element.value; } else {     str = null; } 

You could obviously change the else statement if you want or have no else statement at all, but that is all about preference.

like image 164
Anonymous Avatar answered Oct 05 '22 23:10

Anonymous


Easier and more succinct with ||:

  var str = ((document.getElementById("cal_preview")||{}).value)||"";   var str1 = ((document.getElementById("year")||{}).value)||"";   var str2 = ((document.getElementById("holiday")||{}).value)||"";   var str3 = ((document.getElementById("cal_option")||{}).value)||"";       if (str=="" && str1=="" && str2=="" && str3=="" )       {         document.getElementById("calendar_preview").innerHTML="";           return;         }        if (window.XMLHttpRequest)       {// code for IE7+, Firefox, Chrome, Opera, Safari         xmlhttp=new XMLHttpRequest();       }       else       {// code for IE6, IE5         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");       }        xmlhttp.onreadystatechange=function()       {         if (xmlhttp.readyState==4 && xmlhttp.status==200)           {             document.getElementById("calendar_preview").innerHTML=xmlhttp.responseText;           }       }      var url = calendar_preview_vars.plugin_url + "?id=" + str +"&"+"y="+str1+"&"+"h="+str2+"&"+"opt="+str3;     xmlhttp.open("GET",url,true);     xmlhttp.send();  
like image 38
Andrew Templeton Avatar answered Oct 05 '22 22:10

Andrew Templeton