SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Code:
    Uncaught TypeError: Cannot read property 'value' of null // line 176
    Error in the console log.
    Here is the HTML of my form :
    PHP Code:
    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 :
    Code:
    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!

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,446
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    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.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dujmovicv View Post
    I get the
    Code:
    Uncaught TypeError: Cannot read property 'value' of null // line 176
    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.
    Tab-indentation is a crime against humanity.

  4. #4
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 :
    Code:
    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.

    Full time ADMIN - art community
    Part time coder - dsign


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •