1

Please help me to generate grid system based on user input.

like if user inputs:

3 3 2 4

Then it will create this bootstrap grid:

 <div class="row">
    <div class="span3">...</div>
    <div class="span3">...</div>
    <div class="span2">...</div>
    <div class="span4">...</div>
  </div>

I have tried:

jquery(document).ready( function(){ 
   var userInput = jQuery('.user-input').val();
    //now get number separated by space
    // generate grid, dont know how to get numbers convert them to span(num)
});
6
  • so what have you done so far? Commented Jul 5, 2013 at 1:02
  • where will you add the created bootstrap grid? Commented Jul 5, 2013 at 1:10
  • I will prepend that to a div.container Commented Jul 5, 2013 at 1:12
  • what do these numbers represent and where is the bootstrap part ? Commented Jul 5, 2013 at 1:12
  • @CME64 number represent the bootstrap span size, and space means a new span Commented Jul 5, 2013 at 1:17

2 Answers 2

3

You can use split function.

Example:

var input = "3 3 2 4";
var parts = input.split(" ");
console.log(parts);

Output:

["3", "3", "2", "4"]

And then just loop through the array to add them one by one to your div container (better assign id to the div container though)

This is an example of looping through the array. (reference: Jquery split function)

for (i = 0, l = parts.length; i < l; i++) {
    $('div.container').append('<div class=span' +  parts[i] + '></div>')
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thnaks fro the answer, but can you please tell me how to generate span
PLease give me one simple example of loop, is same as PHP ?
I'm glad I was able to help. :)
can I assign this loop in a variable ?
@HowToPlease you can assign a function to a variable and use it later
2

as for breaking the text and converting it to int numbers:

var input = "3 3 2 4";
var nums = input.split(' ').map(function(i){return parseInt(i);});

then you just have to loop creating the parents first and then the children and children of children like :

for( var i =0; i<nums.length;i++) 
    for (var j =0; j<nums[i];j++) {
        document./*container*/.appendChild(/*your sub-containers*/)

        for (var k =0; k<nums[j];k++) 
            document./*sub-container*/.appendChild(/*your sub-sub-containers*/)
            ...
    }

2 Comments

it seems that I was mislead by the word "grid" in the title, and that the OP wants just to append children to a container ! anyways here is an example for making a grid if the OP changed his mind !
Thanks for the solution, before coming to BS i was trying to do the same thing, but later I migrated to BS. Thanks it is also working +1

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.