0

I'm trying to create a row of multiple divs using Javascript or jquery; say 32 tiny DIVs. Since the size is not fixed, I can simply use HTML. Table cells could also be an alternative, but since I need the click ID, and their color should change later, it is better to go for DIV.

I did it, but the divs are getting vertical instead of horizontal. What I want to do is exactly the notion of Sliding window in TCP. Look at the row of ack'ed packets on top of this simulation: http://histrory.visualland.net/tcp_swnd.html

I want for instance after a button click, the sliding window move to the right one step.

This is my project, simulating the above link for TCP: http://jsfiddle.net/j26Qc/47/

partial code for row of DIVs:

for(var i=1;i<=16;i++){
   $('#table').append("<div id='"+i+"'>"+i+"</div>");
}
4
  • show the code you are using, divs default to a display:block style so they will show underneath each other. to get them to show next to each other change their css display style to inline-block. Commented Feb 13, 2014 at 2:10
  • yeah; EXACTLY! let me bring the code... Commented Feb 13, 2014 at 2:12
  • @PatrickEvans link added! Commented Feb 13, 2014 at 2:15
  • added an answer to show Commented Feb 13, 2014 at 2:16

1 Answer 1

2

You need to change the display style of the divs, they default to display:block, so what you are wanting to do is give them inline-block style.

JS

for(var i=1;i<=16;i++){
   $('#table').append('<div class="inline" id="'+i+'">'+i+'</div>');
}

CSS

.inline {
   display:inline-block;
}

To get them to look like the little blocks in your link, you would need to add additional styling

CSS

.inline {
   display:inline-block;
   width:20px;
   height:20px;
   border:1px solid;
   text-align:center;
}

note that in your fiddle your .table class is only 150px so this would make some of them wrap around, either make the text and size of the divs smaller or make your .table class longer.

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

2 Comments

Donno why the numbers are not shown...Can you please take a look at my fiddle? update 48
@TinaJasmin, cant seem to see the 48 version, here is a updated version that i modified jsfiddle.net/j26Qc/49

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.