javascript - "Uncaught TypeError: Cannot read property 'checked' of null" -


i have problem php , javascript code.

i put simplified code here. first step, want select trip "walking" submit. got trip number 50 , shows check-box. after that, want select check-box , click "select checkbox" button. should call myfunction().

but code inside myfunction() not getting result because got error

uncaught typeerror: cannot read property 'checked' of null

and tried put myfunction() inside <head></head>, did not work well. suggestion guys?

<html> <head></head> <body> <form action = "thispage.php" method = "post"> <select name="typesselect">     <option value="">walking</option> </select> <input type="submit" name="searchformsubmit" value="search" /> </form>  <button onclick="myfunction();">select checkbox</button>  <?php $trip = 1; if(isset($_post['searchformsubmit']))  { $trip = 50; ?> <input type='checkbox' id="mytrip<?php echo $trip;?>">    <?php}  ?>   <script> function myfunction() {  trips = <?php echo json_encode($trip); ?>; var array; initialize(); for(var i=0;i<trips.length;i++){              if(document.getelementbyid("mytrip"+trips[i]+"").checked){                                              $.get('/ajax.php?function=geodata&trip_id='+trips[i], function(data){data=json.parse(data);array=data;addline(data)})         }  }    } </script> </body> </html> 

as @lonewolf217 mentioned in comment looking element doesn't exist.

you should similar this:

for(var i=0;i<trips.length;i++){              var elem = document.getelementbyid("mytrip"+trips[i]+"");              if(elem && elem.checked){                                              $.get('/ajax.php?function=geodata&trip_id='+trips[i], function(data){data=json.parse(data);array=data;addline(data)})         }  }    

whenever using getelementbyid check element returned exists before looking property.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -