1

I have done a fair bit of googling and looking at "you might not need jquery" but still haven't been able to figure out what is wrong.

I have a makeTable function that builds up a html string of elements to form a table. The function works great when I append the output table to the DOM using the Jquery .html method.

Here is the function:

function drawTable () {
  var outputString = '';
  for(var i = 1; i <= 12; i++ ){
    outputString += "<tr>";
    for(var j = 1; j <= 12; j++) {
       outputString += "<td>" + i * j + "</>";
    }
    outputString += "</tr>";
  }
  $("#output").html(outputString);
}

drawTable();

However, when I don't use Jquery and I change $("#output").html(outputString); to document.getElementById('output').innerHTML(outputString); the table doesn't render on the page. I just get a single line of numbers. When I inspect the element, <tr> and <td> elements are not being rendered at all and I don't understand why.

I am confused. How should I render this table in plain JS? What am I not understanding about the innerHTML method ? The functions are identical, I am just changing the line where outputString is appended to the DOM and really don't understand why it is not rendering correctly when I attempt to do it in plain JS. Please enlighten me!

5
  • 4
    "</>" is not an end tag. Commented Dec 17, 2015 at 20:03
  • 5
    innerHTML is not a getter/setter method like jQuery's .html(), it's a property of a DOM element. You need to assign outputString, e.g. element.innerHTML = outputString. Commented Dec 17, 2015 at 20:04
  • @IgorRaush Thanks, I changed the line to document.getElementById('output').innerHTML = outputString. I do not get any console errors. However - none of the '<tr></tr> or <td></td> elements render. When I inspect the element they are not there. All the page displays is a line of numbers. How can I get the table to display using pure JS? Commented Dec 17, 2015 at 22:39
  • @user3562480 Make sure that your element with id="output" is a <table>. See Plunker. Commented Dec 17, 2015 at 22:50
  • Thank you! That fixed. I would have never thought of that. I wonder, why it is necessary to use that specific tag with the JS version but not the JQuery one... weird. Thank you Commented Dec 18, 2015 at 0:05

1 Answer 1

3

@Igor Raush was correct in his comment. You are mis-using the innerHtml. It is not a method that takes a paramenter. It's a property whose value must be directly set with an equal sign.

In plain javascript:

document.getElementById("output").innerHtml = outputString;

In jQuery:

$("#output").html(outputString);
Sign up to request clarification or add additional context in comments.

7 Comments

Hello, and thank you for your comments. However, my Problem is the same as before: None of the <tr></tr> or <td> </td> elements are rendered.
When I run this function :
function drawTable () { var outputString = ''; for(var i = 1; i <= 12; i++ ){ outputString += "<tr>"; for(var j = 1; j <= 12; j++) { outputString += "<td>" + i * j + "</td>"; } outputString += "</tr>"; } document.getElementById('output').innerHTML = outputString }
when I run the above code and change the the line in question to document.getElementById('output').innerHTML = outputString. I do not get any errors, But the Table doesn't render. All that appears is a long string of numbers. When I inspect the element no '<tr></tr>' or '<td></td> ' elements are rendered. What is the problem with pure JS version ? n
You're generating a table without table tags. So it's stripping out all the tr and td tags because there is no table tag to hold them. Check out this fiddle: jsfiddle.net/fedyfztp
|

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.