Skip to content Skip to sidebar Skip to footer

\n Newline Character Won't Work With Javascript

I need to put different information in separate lines, I have a working Javascript library, but I can't force the text output to display a newline. Here is the relevant part of th

Solution 1:

It depends on what you're doing with the text, but my guess is you're rendering it as Html. In that case, you should be using a <br /> tag instead of a \n.

EDIT: It sounds like this doesn't solve your problem, because html tags are encoded and displayed, rather than interepreted as html tags. In that case, you'll either have to modify your tooltip display library, or go with a different library.

Solution 2:

What kind of tootips? If it's a JavaScript library that floats divs over the page you would need to change the library to detect the \n and insert a line break.

If it's simple title attributes, I'm afraid you can't (reliably) have a newline in them at all. Some browsers will render title="a&#10;b" (or the JavaScript equivalent element.title= 'a\nb';) on two lines; others won't.

Solution 3:

Since the tooltip strips html and you cant change that, there aren't a lot of options.

One (rather unpleasant) solution would be to set description to

"My Description1 UNIQUE_STRING_TO_REPLACE My Description2"

Then you can do a search through the DOM to find the node that contains the substring UNIQUE_STRING_TO_REPLACE and replace it with <br />

If you look at the html generated, you may be able to limit your searching to make it faster (your tooltips may all share a class or a similar id.) You'd have to check this yourself, since I don't have access to your html.

It is very hackish (and not very fast) and it would be preferable for you to have the tool-tip allow you to add new lines, but if nothing else works and you must to get this to work, this will at least accomplish your goal.

Solution 4:

You're using a proprietary JavaScript library that you're not allowed/able to edit. You're further not allowed/able to show us the source code of its render function or read it yourself to figure out how it escapes data. Finally, you can't/won't give us the name of this library (assuming it's publicly available) so we can research it ourselves.

You appear to have tried the only reasonable ways that somebody could assume this would work.

The only possible solution is to contact your vendor and demand an answer.

Solution 5:

You're using some kind of library that implements tooltypes. What is it? You should read the documentation for the tooltip library.

Perhaps you can do <p>My description1</p><p>My description2</p> or maybe multipline descriptions are not implemented in the library you are using.

We don't have enough code to know. Maybe show us the list of scripts that's being included in the html.

Post a Comment for "\n Newline Character Won't Work With Javascript"