SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populate div from textarea field

    Hi I want to create a div above every textarea field with its content so it will be printable. However for the first 2 fields the content is the same in the both divs. Can anyone see why?

    // JavaScript Document
    $(document).ready(function(){
    $('textarea').each(function(index) {
    $('<div class="print"></div>').insertBefore('textarea');
    var textindex = $('textarea').val();
    $("div.index").text(textindex);
    });
    });

    Steven

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try this

    Code JavaScript:
    $(document).ready(function(){
        $('textarea').each(function(index) {
            $('<div class="print"></div>').insertBefore($(this));
            var textindex = $('textarea').val();
            $("div.index").text(textindex);
        });
    });

  3. #3
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks but it just does the same thing.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try this

    Code JavaScript:
    $(document).ready(function(){
        $('textarea').each(function(i,v) {
            $('<div class="print"></div>').before($(v));
            var textindex = $(v).val();
            $(v).previous().text(textindex);
        });
    });


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
  •