user7768059
user7768059

Reputation:

JQuery .text() Multi-Line

Is there a way I can turn this obnoxiously long line of text into a paragraph?

Before:

$('.ContText-1').text('For a business to thrive, it needs a team of people who are dedicated to its success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of that team for each of their clients.');

After:

$('.ContText-1').text('
             For a business to thrive, it needs a team of people who are dedicated to its 
        success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of 
        that team for each of their clients.
');

If I run the paragraph one I get this error: Uncaught SyntaxError: Invalid or unexpected token
Its referencing to the single quotation mark after the (.

Upvotes: 1

Views: 817

Answers (3)

Rory McCrossan
Rory McCrossan

Reputation: 337560

Assuming you're attempting to break the string in your code, not the output, you can use a template literal by delimiting the string with `:

$('.ContText-1').text(`
  For a business to thrive, it needs a team of people who are dedicated to its 
  success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of 
  that team for each of their clients.
`);

Note that this is completely unsupported in any version of IE, although works in every other modern browser - even Edge.

Alternatively you can append each line separately:

$('.ContText-1').text(
  'For a business to thrive, it needs a team of people who are dedicated to its ' + 
  'success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of ' + 
  'that team for each of their clients.'
);

Upvotes: 1

Stefan Dochow
Stefan Dochow

Reputation: 1454

Instead of enclosing the String with ' , try to use ` .

At least in newer iteration of JS this should work fine.

UPDATE:

Actually, this had been covered before: Creating multiline strings in JavaScript

Upvotes: 0

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

instead of text() method use html() method and use <br/> to break the line

$('.ContText-1').html('<br/>For a business to thrive, it needs a team of people who are dedicated to its <br/>success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of <br/> that team for each of their clients.');

Upvotes: 0

Related Questions