confused and dazed Posted January 6, 2012 Share Posted January 6, 2012 Hello internet. I am having an issue with the following code working.CODE:<script type="text/javascript">window.onload = function() {for (var i = 0; i < Tload.length; i++){document.getElementById(Tload.id).value = Tload.value;}</script>The error code I am getting is:'docement.getElementById(…)' is null or not an object Link to comment Share on other sites More sharing options...
confused and dazed Posted January 6, 2012 Author Share Posted January 6, 2012 Here is a little more code... I am using JSON to send values to the browser $result = mysql_query("SELECT r1, r2, r3 FROM database1 WHERE grp='a'", $con);$rows = array();while ($row = mysql_fetch_assoc($result)) { $rows[] = array( 'id' => 'b1', 'value' => $row['r1'] ); $rows[] = array( 'id' => 'b2', 'value' => $row['r2'] ); $rows[] = array( 'id' => 'b3', 'value' => $row['r3'] ); }$json = json_encode($rows);echo '<script type="text/javascript">var Tload = '.$json.';</script>';?><script type="text/javascript">alert(typeof Tload); // check if Tload var existsalert(Tload.length); // check length of arraywindow.onload = function() {for (var i = 0; i < Tload.length; i++) { document.getElementById(Tload.id).value = Tload.value; }}; The alerts say that it is an object with 3 for length... What am I doing wrong? It will not load the town names... Link to comment Share on other sites More sharing options...
regicidedelferoz Posted January 6, 2012 Share Posted January 6, 2012 try this document.getElementById(Tload[i]).value = Tload[i].value; Link to comment Share on other sites More sharing options...
dsonesuk Posted January 6, 2012 Share Posted January 6, 2012 Both examples should work? I suppose you have created form inputs with id ref b1, b2, b3 otherwise it will produce null error, as it is trying to apply values to elements with specific ID ref that it cannot find. Link to comment Share on other sites More sharing options...
confused and dazed Posted January 6, 2012 Author Share Posted January 6, 2012 Thanks for the suggestions however I am still getting the same error. Could it be my browser? structure of my code maybe? Link to comment Share on other sites More sharing options...
confused and dazed Posted January 6, 2012 Author Share Posted January 6, 2012 I have absolutely created those ids in the form... this is so frustrating... Link to comment Share on other sites More sharing options...
dsonesuk Posted January 6, 2012 Share Posted January 6, 2012 View the page source do you see the town names listed in the php generated JavaScript, It will still list number of 3 items in the array because array and items b1 to b3 are generated manually, but the town name items could be still empty! Link to comment Share on other sites More sharing options...
confused and dazed Posted January 6, 2012 Author Share Posted January 6, 2012 Source shows var Tload = [{"id":"b1","value":"DET"},{"id":"b2","value":"CHI"},{"id":"b3","value":"MIA"}];The alerts say it is an object with three in length so its not that it is not seeing an object it is saying there is no value but there is a value... Link to comment Share on other sites More sharing options...
dsonesuk Posted January 6, 2012 Share Posted January 6, 2012 Then it should work, arrays values items are listed, input form elements have unique id ref of id="b1", id="b2 and" id="b3", so theres not much else we can suggest unless! we see the whole code and not bits, and pieces (I think i've mentioned this before) so we can identify maybe a conflict that is preventing this code from working correctly. Link to comment Share on other sites More sharing options...
thescientist Posted January 6, 2012 Share Posted January 6, 2012 I have absolutely created those ids in the form... this is so frustrating... show us that part of the code too, then. Link to comment Share on other sites More sharing options...
confused and dazed Posted January 6, 2012 Author Share Posted January 6, 2012 Dammit - I am so angry with myself all this trouble and it boils down to spelling an id wrong in the form... bs and ds look the same to me I guess... sorry for troubling you all on this one... I will try to keep my posts to content that actually need help. Link to comment Share on other sites More sharing options...
confused and dazed Posted January 7, 2012 Author Share Posted January 7, 2012 dsonesukthescientist,Please understand that I don't post a lot of my code because I want to give just enough for you to understand the issue I am having and then help with links to things to read or suggestions for the code I posted. I would like the satisfaction of coding in the solution myself. Link to comment Share on other sites More sharing options...
thescientist Posted January 7, 2012 Share Posted January 7, 2012 right, but when you post javascript that is reliant on the markup (i.e. using ID's and DOM manipulation) it's pretty much in good form to post the markup too, because they are coupled at that point and thus would be helpful for seeing the bigger picture for us when debugging; as this case plainly showed. If you were just writing a JS function to compute some sort of algorithm, or confirming some sort of advanced coding technique, then yeah, only the JS would really be required. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.