1

I am trying to use 
 while creating a string with the new line.

Here is what I tried.

tooltip = "User is already present."+ 
 + props.email;

But this is not working .while showing the tooltip.

Can any one help me with this ?

5
  • Add it inside the string directly. User is already present 
 Commented Jul 19, 2019 at 10:35
  • No it does not work. I just tried Commented Jul 19, 2019 at 10:35
  • how is tooltip used? Commented Jul 19, 2019 at 10:36
  • Are you adding it in title attribute? Commented Jul 19, 2019 at 10:38
  • You can use \n(new line escape squence). Commented Jul 19, 2019 at 10:38

6 Answers 6

2

In Javascript, it needs to be done by using \n.

var foo = "Bob\nis\ncool.";

tooltip = "User is already present.\n"+ props.email;
Sign up to request clarification or add additional context in comments.

Comments

1

String templates allow to use new lines:

tooltip = `User is already present.
${props.email}`;

Comments

1

For example, you want to have the following text:

Hi!
I like JS

You can use \n:

const myMessage = 'Hi!\nI like JS;'

Or template strings:

const myMessage = `Hi!
I like JS`;

Comments

1

Use template literals in javascript it makes it more easier in all cases you do not need to add additional "\n" each time to add new line just right a paragraph with in quotes for help see this link it describe it very clearly

https://flaviocopes.com/javascript-template-literals/

Comments

1

you need to set the html and get the html, like in below code you can remove first and last line and can use it as it is.

var props={email:'[email protected]'}
var htmlObject = document.createElement('div');
htmlObject.innerHTML = "User is already present. 
" + props.email;
tooltip = htmlObject.innerHTML ;
alert(tooltip);

Comments

1

You must use

\n

in JavaScript code and

white-space: pre-line;

in CSS to display new line

3 Comments

I think the css value is right, but the property is white-space. The full rule would be be white-space: pre-line.
Sorry, my mistake. You're right. The correct css should be white-space: pre-line. I don't know how to edit my post.
I think I was able to do it for you—it should show up in just a moment or two.

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.