1

I've seen lots of questions about preserving line breaks in textarea inputs, but I need the opposite. I suspect the answer to my question is not going to be a good one, but...

I'm working with a preformatted e-card (in Convio, if that helps anyone). I can add any text and HTML I want, but the data that's coming in from the form is via Convio tags and can't be changed.

Here's what I'm talking about. The textarea input is:

Hello,

How are you?

This would come into the e-card as:

Hello,<br />
<br />
How are you?

In other words, it does insert the break tags, but it also puts in an actual line break after each tag. No problem, right? That will display properly since the email reader ignores the actual line breaks in the HTML, but unfortunately additional information is prepended to the textarea input, so what comes into the e-card is really:

A message from Mr. John Smith [email protected].<br />

Hello,<br />
<br />
How are you?

With the name and email address coming from other fields in the form. This, of course, displays as:

A message from Mr. John Smith [email protected].    
Hello,

How are you?

Here's the issue: I hate that first line. I mean I really hate it. The info is already on the e-card elsewhere and I don't want it there. It's too unfriendly, it's visually unappealing on the e-card, it almost always wraps to a second line (which looks even worse, particularly without space between that and the first line from the textarea), and it just makes me want to cry.

If everything came in as a string without the actual line breaks, I'd have no problem slicing and dicing the text to remove the first line. I could edit the input form to remove the actual breaks in the textarea input, but those two line breaks after the "A message from..." message are killing me. I can't figure out how to make the input string palatable for javascript.

I am limited to javascript--yes, I know this would be simple to do in PHP, but I don't have that option--and my webmaster and I have pretty much come to the conclusion that we can't do what I want to do. I had to double-check, though, and I'd love for someone to prove me wrong.

3 Answers 3

1

grab the innerHTML of which ever element has the message, and substring out the first line.

http://jsfiddle.net/Q8gu8/4/

Sign up to request clarification or add additional context in comments.

3 Comments

OK, I had the same problem with this as I did with hafichuk's response below, but it gave me a great starting point. IE didn't see the line break AND it changed the break tags to the old form (i.e. <BR>). Thanks for the help! This was my first try at javascript. jsfiddle.net/Q8gu8/9
Well, it worked in the test environment. When I drop it in the template it gets completely screwed up. I give up. Thanks for the help, guys. I wish I could uprep ya, but my own reputation isn't high enough to do so.
If you could post some code from the template you are using, I'm sure we could get it working for ya... thats why we are here :)
1

You can match a line-break using "\n" (or \r for a carriage return). So, if I have the following string:

Foo
is
An
Apple

I can remove the first line like so:

// assume the string is stored in variable `str`
str = str.replace(/^.+?\n/, '');

... which means I'm left with:

is
An
Apple

/^.+?\n/ is a regular expression which means: match everything up to and including the first line break.

Comments

0

You should be able to use a regex with replace()

See this fiddle.

HTML:

<div class="replace">
    A message from Mr. John Smith [email protected].<br />

Hello,<br />
<br />
How are you?
<div>

JS:

var haystack = $('.replace').html();

var regex = /A message.*/;
$('.replace').html(haystack.replace(regex, ""));

Output:

Hello,

How are you?

Note that this is using JQuery for the selector.

2 Comments

Hmm... I'm not getting any output on the fiddle.
I take it back: It works well on Firefox. I'm required to use IE at work, and it didn't work there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.