0

I'm trying to add line breaks to the response in this already working javascript:

if (/example text/i.test(text)) return "example response";

I've tried all of the following and none have worked:

if (/example text/i.test(text)) return "example response 1/nexample response 2";
if (/example text/i.test(text)) return "example response 1" + '/n' + "example response 2";
if (/example text/i.test(text)) return "example response 1<br />example response 2";

etc etc

Is there any easy way to accomplish this inline without adding vars or more complexity to the rest of my code?

4
  • 2
    A line break is \n, not /n. Commented Mar 24, 2013 at 11:04
  • Where is this string going to be used ? As HTML ? Commented Mar 24, 2013 at 11:05
  • Argh, sorry that's what i meant. Commented Mar 24, 2013 at 11:05
  • @user2204323 I'm not sure I got your last comment. Did Felix's answer work for you ? Commented Mar 24, 2013 at 11:06

1 Answer 1

1

If your string is going to be displayed as HTML, you may do this :

if (/example text/i.test(text))
    return "example response 1<br"+">example response 2";
Sign up to request clarification or add additional context in comments.

5 Comments

Didn't work, maybe my code is rendering as straight plain text instead of as html? $('div').thedivclass(function(text){the if/return code above}, null, type); Is this screwing up how it renders?
Well, what does do thedivclass ?
$('<p class=response>').appendTo(self),handler(this.textContent || this.innerText); The code you suggested renders this if it helps: <p class="response"><span>example response 1&lt;br&gt;example response 2</span></p>
Ok, so it uses it as text. There is no direct solution, you'd better change your code so that it doesn't use this.textContent || this.innerText but this.innerHTML.
this.innerHTML as you directed worked, it's respecting html markup now, thx!

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.