0

I am trying to create a table dynamically as soon as a page loads. In the code below, I get a response when I click the button, but the table is not displayed on the page. What's wrong with the code below? I have looked at other discussion threads on this topic, but none have helped.

The code in the Javascript file is as follows:

    function showalert() {
            alert('what?');
    }

    function displayGuides() {
            var mgdCell, mgdRow, mgdTable;

      mgdTable = document.createElement('table');
      mgdRow = mgdTable.insertRow(0);
      mgdCell = mgdRow.insertCell(0);
      mgdCell.innerHTML = "1111";
      mgdCell = mgdRow.insertCell(1);
      mgdCell.innerHTML = "2222";
      mgdCell = mgdRow.insertCell(2);
      mgdCell.innerHTML = "3333";
      mgdCell = mgdRow.insertCell(3);
      mgdCell.innerHTML = "4444";
      mgdCell = mgdRow.insertCell(4);
      mgdCell.innerHTML = "5555";
      document.getElementByID('mgdTable').appendChild(mgdTable);
    }

    function mgdUserActions() {
      var create = document.getElementById('create');
      create.onclick = showalert;
      displayGuides();
    }

    window.onload = mgdUserActions;
4
  • 1
    always check your javascript console for errors please, and post those errors if u get any. ok, now i got it right: just change displayTable; to displayTable(); Commented Jun 13, 2013 at 14:36
  • Well, for one, this isn't the best way to build a table dynamically. Try using templates, like Mustache or Handlebars. Commented Jun 13, 2013 at 14:40
  • I am trying to display the table as soon as the page loads. I don't need the user to click the button to display the table. Commented Jun 13, 2013 at 14:41
  • right. i saw that too late. but your code should work as far as i can see. check your javascript console for errors if it doesn't Commented Jun 13, 2013 at 14:42

1 Answer 1

2

Your call in displayGuides to document.getElementByID should be document.getElementById. The 'D' in ID should be 'd' Id.

Check out the fiddle of awesomeness

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Ryaminal. The code worked after I made the change you suggested. My Javascript coding skills are a bit rusty, and I wasted a day on debugging this :-(
@AnilBhat Glad it worked. I found it by hitting F12 and Chrome's Developer Tools magically told me the exact problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.