Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: ncaught TypeError: Cannot read property 'split' of null

Tags:

javascript

I dont understand what is happening here? any suggestions? This is happening on this line :-

var explode = gregorianDate.split('-');

Here is full function.

function updateCalendarDay(){ 
    if (document.getElementById('gregorianDateOutput') != null) {
       var gregorianDate = document.getElementById('gregorianDateOutput').value;
    }else{
       var gregorianDate = null;
    }
    if(gregorianDate != ""){
       var explode = gregorianDate.split('-');
       var year = explode[0];
       var month = explode[1];
       var day = explode[2];
       document.getElementById('month').value = month;
       document.getElementById('year').value = year;
       var ajax = new GLM.AJAX();
       var url='calendarAjax.php?month='+month+'&year='+year+'&day='+day;
       ajax.callPage(url, showSubscribeResult, "GET");
    }           
}
like image 961
ads man Avatar asked Jan 11 '23 00:01

ads man


1 Answers

As @Jasper suggest you can assign empty String if not found:

var gregorianDate = "";

instead of

var gregorianDate = null;

EDIT:

Also you can check just by value:

if( gregorianDate )

And as Element value property always return's String you will get this only if value is present

like image 105
antyrat Avatar answered Feb 08 '23 14:02

antyrat