/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@weblaserauthorNov 10.2010 — 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";
Copy linkTweet thisAlerts:
@FangNov 10.2010 — [CODE]var statement = "SELECT all data,FROM Departments,WHERE grouphead = 'Milo',GROUP BY rooms";
function write2area(statement){
var Query = statement.split(',').join('rn');
document.getElementById('textarea1').value = Query;
}
[/CODE]
Copy linkTweet thisAlerts:
@weblaserauthorNov 10.2010 — 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('rn');

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();
Copy linkTweet thisAlerts:
@FangNov 11.2010 — Ask in the Perl forum how to include JavaScript in Perl
Copy linkTweet thisAlerts:
@FangNov 11.2010 — 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
;
[/CODE]
Copy linkTweet thisAlerts:
@weblaserauthorNov 11.2010 — 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
×

Success!

Help @weblaser spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...