Jump to content
confused and dazed

Document.getelementbyid Is Not Working For Me...

Recommended Posts

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

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
I have absolutely created those ids in the form... this is so frustrating...
show us that part of the code too, then.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...