/    Sign up×
Community /Pin to ProfileBookmark

Textarea line capture

Let me start by saying that I am very new to javascript and programming in general so please be a specfic as you can with your answers. What I am trying to do is use javascript to grab each line (word wrapped at 95 characters) in a textarea and place it into an array so that I can pass the variables to the page that follows the form submission without splitting any words in half. So I guess this means breaking down the textarea into textarea1, textarea2, textarea3, etc. Is this even possible? Any help is greatly appreciated.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinMar 22.2010 — Let us see your textarea tag including inline style, CSS style and attributes.
Copy linkTweet thisAlerts:
@burtont53authorMar 22.2010 — The textarea code is really as simple as it gets...

<textarea name="textarea" cols="95" rows="10"></textarea>
Copy linkTweet thisAlerts:
@mrhooMar 22.2010 — If you want to ignore all newlines and tabs and just break on spaces,

build the array by finding the last space in each 95 character chunk.



[CODE]function wordwrap(s, L){
// trim leading and trailing spaces and convert
// all whitespace sequences to single spaces:
s= s.replace(/(^s*|s*$)/g,'').replace(/s+/g,' ');
var a= [], ax;
while(s.length> L){
ax= s.substring(0, L).lastIndexOf(' ');
if(ax== -1) ax= L;
a[a.length]= s.substring(0, ax);
s= s.substring(ax);
}
if(s.length) a[a.length]= s;
return a;
}[/CODE]

//test

var s= document.getElementsByName('textarea')[0].value || '';

[B]alert(wordwrap(s,95).join('n'));[/B]
×

Success!

Help @burtont53 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.18,
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,
)...