SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot godfather999's Avatar
    Join Date
    Sep 2011
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to dynamically create div using jQuery?

    Hi,

    can you tell me how to dynamically create divs using jQuery. For example, how to make three divs inside body, so every next div will be removed from left 20px?

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    That's pretty basic jQuery stuff, it's explained pretty well in the jQuery docs.
    But here ya go:

    Code:
    $(function() {
        for(i=0; i<3; i++) {
             $('<div>').css({marginLeft: 20}).appendTo('body');
        }
    });

  3. #3
    SitePoint Zealot godfather999's Avatar
    Join Date
    Sep 2011
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First i created element with width 100% one below another with jQuery using for loop. Then in another for loop a created small boxes horizontally. But that only appended in first div not in every else.

    http://img192.imageshack.us/img192/3599/boxesn.png

    Do you know what I mean? How it is possible to do that?

  4. #4
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $('#button').click(function() {
    for(int i=0;i<=3;i++){
    $('body').append('<div id="someid">content</div>');
    }
    });


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •