PoorYorick PoorYorick - 4 months ago 7
Javascript Question

jquery does not display content

I this simple bit of test code to loop through and display content from an array. When I run it in my browser, it doesn't display or run any errors.

<html>
<head>
<title>jquery each test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script type="text/javascript">
var arr = ["One", "Two", "Three", "Four", "Five"];
var task = "";
$(document).ready(
$.each(arr, function(index, value) {
task += value + "<br>";
$('div.content').html(task);
}));
</script>
</head>
<body>
<div class="content"></div>
</body>
</html>

Answer

Change your ready function code with following line

 $(document).ready(function(){
          $.each(arr, function(index, value) {
            task += value + "<br>";
            $('div.content').html(task);
          })});
Comments