views:

48

answers:

6

Any idea what's wrong with my code?

var pageLimit=30;

$(document).ready(function() {
 for(var i = 1; i <= pageLimit; i++) {
  $('#test').append('<div id="page' + i + '" class="touch">TESTING</ div>' )
 }
}

What I want is to have that function create as many divs in the body as the pageLimit value. So if someone were to go into the code and change the pageLimit to 50, it would create 50 div tags.

in the body tags, all I have is the test div. I wanted to put it into the body, without inserting it into any other divs. So I tried to replace #test with body, didn't work.

Please help! Thanks.

EDIT: Sorry, I have the ); in my original code I just forgot to copy it here! Yeah, the
tags were before I knew how to insert code into this... lol Sorry. I have ); in my original code, it still doesn't work.

+4  A: 

Missing ");" after the last }.

$(document).ready(function () {
    for (var i = 1; i <= pageLimit; i++) {
        $('#test').append('TESTING');
    } 
});
Andy Robinson
A: 

How about document.body.innerHTML += 'TESTING'; ?

Fosco
This errors with "$(document).body is undefined". As you've wrapped the document as a JQeury object. All you'd need to do is document.body... no need for JQuery.
Andy Robinson
ah yes, nice catch thanks.
Fosco
+1  A: 

http://jsfiddle.net/Q6Lnw/2/

You were missing the end of the ready function's )

Ian Wetherbee
+1  A: 

Your issue is a simple syntax problem. You were missing ")". Always make sure to add in line endings too. This works:

$(document).ready(function () {
         for (var i = 1; i <= pageLimit; i++) {
             $('#test').append('TESTING');
         }
     });
Zacho
Also: here is the optimized code. This makes one DOM manipulation which will greatly increase performance. var pageLimit = 30, testing = 'TESTING'; $(document).ready(function () { for (var i = 1; i <= pageLimit; i++) { testing += testing; } $('#test').append(testing); });
Zacho
A: 

It seems it's still a syntax problem, you have simple quotes then back quotes in your element string, try removing the backquotes. And of course, make sure you have <div id="test"></div> in your html.

$(document).ready(function() {
   for(var i = 1; i <= pageLimit; i++) {
     $('#test').append('<div id="page' + i + '" class="touch">TESTING</div>' )
   }
});

Unless that's another typo in your question.

UberNeet
No That is there because when you want to include quotes inside of quotes, the outer quotes should be single quotes, while the inside ones should be double quotes.You can see the differences if you use notepad++.. At least thats how my teacher taught me!
Annie
I am talking about the back quote (`). As you can see in the code I placed, I have the single and double quotes too.
UberNeet
A: 
$('body').append('<div>TESTING</div>')

Should work. What does your not-working code look like?

Lourens