0

Alright I'm a little lost.

I've a page that pulls a value out of a database which is then being loaded into a variable, ticketNum. Basically I just need to generate the same div/content every time based on this number. I'm at a loss as it how to do it.

If the ticketNum is 10, I need 10 divs, if it's 3, I need 3 and so on.

$(document).ready(function(){
 var ticketNum = 10;

 if (ticketNum > 0) {

   } else {

   }

});
2
  • Try to do this using your server-side language if possible. Commented Feb 11, 2011 at 19:07
  • 1
    Why should they try to do it via a server side language? Just simple dom manipulation. Commented Feb 11, 2011 at 19:13

2 Answers 2

3

http://jsfiddle.net/5KHaq/

$(document).ready(function(){ 
    var ticketNum = 10;

    for(var i = 0; i<ticketNum;i++){
        $('<div></div>').appendTo('#container').text("Div number " + i);   
    }
});

Is that what you are looking for?

Sign up to request clarification or add additional context in comments.

2 Comments

Our answers are, essentially, the same: so +1 =)
heh yeah, we just happen to hit submit at roughly the same time.
2

You can use a for loop:

if (ticketNum > 0) {
    for (i=0; i < ticketNum; i++) {
        $('<div id="addedDiv_num' + i + '" />').text('Div number:  ' + i).appendTo('#containerElement');
    }
}

JS FIddle demo.

2 Comments

Awesome thanks. I know this was probably dead simple for ya, but as designer dabbling in dev it's a huge help.
@OD_WebDev: no problem at all (we all started learning something at least once...) =)

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.