2

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.

5
  • Can you please show some html code - what do you have, and what do you want to have when your code has finished manipulating it? Commented Jul 16, 2010 at 14:14
  • add 4 spaces before all code lines, and it will appear properly on stackoverflow. Commented Jul 16, 2010 at 14:17
  • To add HTML, edit your question (there should be an edit link when you look at it) and make the code section a bit of code by highlighting it and clicking the 101010 button. Commented Jul 16, 2010 at 14:18
  • I'll assume the <br/> tags aren't in your code either? Commented Jul 16, 2010 at 14:21
  • use a good debugger like firebug to get a good description of the javascript error message... Commented Jul 16, 2010 at 14:21

6 Answers 6

6

Missing ");" after the last }.

$(document).ready(function () {
    for (var i = 1; i <= pageLimit; i++) {
        $('#test').append('TESTING');
    } 
});
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

1

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');
         }
     });

1 Comment

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); });
1
$('body').append('<div>TESTING</div>')

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

Comments

0

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

1 Comment

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.
0

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.

2 Comments

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!
I am talking about the back quote (`). As you can see in the code I placed, I have the single and double quotes too.

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.