www.webdeveloper.com
Results 1 to 7 of 7

Thread: Displaying a single string value in a textarea as multilined

  1. #1
    Join Date
    Nov 2010
    Posts
    4

    Displaying a single string value in a textarea as multilined

    Hello,

    Using js, I would like to write a single string of several phrases into a textarea such that each phrase is aligned in order, one above the other.
    I tried a function that receives the string argument and splits it into an array. With a 'for-loop', elements of the array are assigned in turn to the textarea value. This gives me only the last element ('GROUP BY room') displayed , since it was the last assigned. My placing of a '+', a break("<br>") or a newline(\n) didn't help.
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    To get the idea, the following piece of script displays the string as broken, stacked, but not in the textarea where I want it:

    $Statement = "SELECT all data,
    FROM Departments,
    WHERE grouphead = 'Milo',
    GROUP BY rooms";


    function write2area(statement){
    var Query = new Array();
    Query = statement.split(/,/);
    for(i=0;i<Query.length;i++){
    document.write(Query[i] + "<br>");
    }
    }
    ……………………………
    Which gives the output (below the textarea):

    SELECT all data,
    FROM Departments,
    WHERE grouphead = 'Milo',
    GROUP BY rooms
    If you could suggest a way to break and stack a sting in a textarea, I would be most thankful.

    weblaser

  2. #2
    Join Date
    Nov 2010
    Posts
    4

    Correction for initial post(Dispalying a single string...)

    The initial string argument is a one liner and should have been written as follows:

    $Statement = "SELECT all data,FROM Departments,WHERE grouphead = 'Milo',GROUP BY rooms";

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var statement = "SELECT all data,FROM Departments,WHERE grouphead = 'Milo',GROUP BY rooms"; 
    function write2area(statement){
    var Query = statement.split(',').join('\r\n');
    document.getElementById('textarea1').value = Query;
    }
    At least 98% of internet users' DNA is identical to that of chimpanzees

  4. #4
    Join Date
    Nov 2010
    Posts
    4
    Hello Fang,
    Thank you for the help with this javascript.

    I almost got it conceptually: It seems that after splitting the string, it is reassembled with each element 'new lined' as it is rejoined. This keeps everything within the single, textarea, value.
    My only problem now is to see it work. The program written below (can check the Perl vs. the Source page) should, I think, be sufficient to test the function you suggested, but it doesn’t go. Could you steer me yet a bit further down the road?
    Thanks.
    _______________________________________
    #!/usr/local/bin/perl -w

    use CGI qw(:standard);
    use CGI::Carp qw(fatalsToBrowser);
    use strict;
    my $query = new CGI;

    my $JSCRIPT=<<EOF;

    var statement = "SELECT all data,FROM Departments,WHERE grouphead = 'Milo',GROUP BY rooms";
    function write2area(statement){
    var Query = statement.split(',').join('\r\n');
    document.getElementById('textarea1').value = Query;
    }

    EOF
    ;

    #SETTING UP A FORM FOR INPUT:
    print $query->header();

    print $query->start_html(-title => 'Selection ',
    -BGCOLOR=>'lavender',
    -script=>$JSCRIPT);
    print $query->startform(-name=>'sql_tool');

    print $query->textarea(-name=>'mysqlQuery',
    -default=>'SELECT * FROM Departments',
    -id=>'textarea1',
    -rows=>7,
    -cols=>70),p;

    print $query->button(-name=>'trigger',
    -value=>'breaker',
    -onClick=>'write2area()'
    );

    print $query->endform();

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Ask in the Perl forum how to include JavaScript in Perl
    At least 98% of internet users' DNA is identical to that of chimpanzees

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Installed Perl, this should solve the problem
    Code:
    my $JSCRIPT=<<EOF;
    
    var statement = "SELECT all data,FROM Departments,WHERE grouphead = 'Milo',GROUP BY rooms";
    function write2area(){
    var Query = statement.split(',').join('\\r\\n');
    document.getElementById('textarea1').value = Query;
    }
    
    EOF
    ;
    At least 98% of internet users' DNA is identical to that of chimpanzees

  7. #7
    Join Date
    Nov 2010
    Posts
    4

    In conclusion:

    Hello Fang,
    Bravo!
    Touching up my test script with 'breaker' button (reply#4 above) per your pointed suggestion, the string is displayed in the textbox as desired.
    Thank you for your time and attention.
    weblaser

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles