Count
Count

Reputation: 1423

unable to create table using jquery

Below is my HTML fine. I am getting a blank page as output

<html>
<head>
<script src="jquery-1.11.0.min.js">
</script>
<script>
var content = "<table>"
for(i=0; i<10; i++){
content += '<tr><td>' + 'answer' +  i + '</td></tr>';
}
content += "</table>"
$('#myTable').append(content);

</script>
</head>
<body>
<div id="myTable"></div>
</body>
</html>

the output that i expect is answer 1 answer 2 ....... answer 3

Upvotes: 0

Views: 77

Answers (3)

Aniruddha
Aniruddha

Reputation: 308

ID change is the only thing you need to do:

$('#myTable').append(content);

Upvotes: 0

flauntster
flauntster

Reputation: 2016

Your js isn't waiting for the document to load, and missing 2 semi-colons.

Try changing your js to:

<script>
$(document).ready(function(){ 
  var content = "<table>";
  for(i=0; i<10; i++){
    content += '<tr><td>' + 'answer' +  i + '</td></tr>';
  }
  content += "</table>";
  $('#here_table').append(content);
}); 
</script>

Upvotes: 2

Satpal
Satpal

Reputation: 133403

Wrap your code in document-ready handler. it specify a function to execute when the DOM is fully loaded. like

$(document).ready(function() {
  //Your code
});

You should read When should I use jQuery's document.ready function?

Also you are using incorect id. use

$('#myTable').append(content);

instead of

$('#here_table').append(content);

DEMO

Upvotes: 2

Related Questions