简体   繁体   中英

Geo Location Trace getElementById not working

I am working on a challenge by freecodecamp, Task is to show the local weather and hence I have to get the location of the user. I can get the location from here but after printing that i was trying to getElementById of a div i have printed using JS which gives null in response. I want to get the key value pair so that i can do stuff with them. Here is my code:

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Location Trace | freecodecamp Challanges</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> </head> <body> <div id="GeoResults"></div> <script> $.getJSON("http://ip-api.com/json/?callback=?", function(data) { var table_body = ""; var count = 0; $.each(data, function(k, v) { //JSON.stringify(j); // '{"name":"binchen"}' table_body += '<div id=Coun_'+count+'>'+v+'</div>'; //table_body += "<tr><td id='FC_"+count+"'>" + k + "</td><td><b id='SC_"+count+"'>" + v + "</b></td></tr>"; count++; }); $("#GeoResults").html(table_body); }); </script> <script> var x = document.getElementById('Coun_1') /*= 'Dooone!!!'*/; console.log(x); </script> </body> </html> 

Thanks in Advance!

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Location Trace | freecodecamp Challanges</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> </head> <body> <div id="GeoResults"></div> <script> $.getJSON("http://ip-api.com/json/?callback=?", function(data) { var table_body = ""; var count = 0; $.each(data, function(k, v) { //JSON.stringify(j); // '{"name":"binchen"}' table_body += '<div id=Coun_'+count+'>'+v+'</div>'; //table_body += "<tr><td id='FC_"+count+"'>" + k + "</td><td><b id='SC_"+count+"'>" + v + "</b></td></tr>"; count++; }); $("#GeoResults").html(table_body); var x = document.getElementById('Coun_1').innerHTML; /*= 'Dooone!!!'*/; console.log(x); }); </script> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM