Uncaught TypeError: Cannot read property 'value' of null

Hi guys,
I need your help to find out what went wrong with my previously working script… I get the


Uncaught TypeError: Cannot read property 'value' of null // line 176

Error in the console log.
Here is the HTML of my form :


echo "
<input type='text' class='input-small' data-datepicker='datepicker' id='planned_finish_date_".$i."' name='planned_finish_date_".$i."' />
<span class='add-on'><i class='icon-calendar'></i></span>";

and here is the JS :


for (var i=1;i<=nr_of_tasks;i++)
  {
  var planned_finish_date = encodeURI(document.getElementById('planned_finish_date_'+i).value); // this is the line 176
  var FinishDate = planned_finish_date;
  console.log (FinishDate);
  }

I get the Finish Date correctly in the console log but the script is not working because of the mentioned error in line 176… Any suggestion or help will be highly appreciated!

Check the value of i in your JS loop… sounds like it might be running one time too many and it’s trying to call .value on a non-existant element.

It means the element with ID ‘planned_finish_date_’+i doesn’t exist, probably because either the loop condition is incorrect or the code is executing prematurely, but that can’t be determined from the code shown.

Thanks for the tips. I didn’t notice that I wanted to assign a value to the variable even if the checkbox was not checked… This solved my issue :


if (this.checked == 1) {
	var planned_finish_date	= encodeURI(document.getElementById('planned_finish_date_'+i).value);
	var FinishDate = planned_finish_date;
	//console.log (FinishDate);
}

Thanks again. Regards.