jQuery tooltip add line break
Solution 1
You can use the 'content' option of the tooltip widget. See also:
http://jqueryui.com/tooltip/#custom-content
Short example:
$(function() {
$( document ).tooltip({
content: function() {
return 'foo'
}
});
});
Solution 2
There's a pure CSS solution. Use \n for newlines, and add this CSS style:
.ui-tooltip {
white-space: pre-line;
}
You can also use pre or pre-wrap instead of pre-line, if you want to preserve whitespace. See https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
Solution 3
just use the entity
for a linebreak in a title attribute.
Solution 4
This is my trick to do it with latest jquery / jqueryui (assuming all of the items you want to have tooltips have class 'jqtooltip', they have title tags, and the title has a pipe character for a line separator:
$('.jqtooltip').tooltip({
content: function(callback) {
callback($(this).prop('title').replace('|', '<br />'));
}
});
Solution 5
You could type your HTML directly into the Title attribute and then simply call the following:
$(document).tooltip({
content: function (callback) {
callback($(this).prop('title'));
}
});
This way your HTML is rendered instead of escaped and literally written.
ChrisGP
life is great...as long as you look at it with the right kind of glasses...;)
Updated on January 12, 2020Comments
-
ChrisGP over 4 years
So, I want to take the content of a div in which I have several
<br/>
, and then pass it as a title attribute using jQuery tooltip widget. I want the lines to appear one beneath the other inside the tooltip. thx. the code so far is:CSS
.Lines { width: 125px; height:20px; overflow:auto; }
JavaScript
$(function () { $(document).tooltip(); $(".Lines").hover(function () { IaTxt = $(this).html() $(this).prop('title', IaTxt) }) });
HTML
<div class="Lines"> First line. <br/>Second line. <br/>Third line! <br/>Fourth line? </div>
-
ChrisGP over 11 yearshow specifically would I implement that in my code? i set the title attribute via jQuery
-
Ravi Gadag over 11 yearsadd the text, where you want the line break. or replace with the <br/> tag
-
ChrisGP over 11 yearsI don't know if you understood my initial question. I want to take the content of the div [see example div] and display it as a title with jQuery tooltip plugin. in that div i already have
<br/>
and I want the line breaks to appear in the tooltip where the<br/>
are. -
Ravi Gadag over 11 yearsthats only i told. u replace the <br/> with
yourVariable.replace("<br/>"," ");
-
ChrisGP over 11 yearscould you give a practical example, that would achieve my goal? thx
-
Teun Lassche over 11 yearsThe string 'foo' can in your case be replaced by Iatext
-
Ronen Festinger almost 11 yearsAlso I had to replace /n with <br/> in my text before setting the title attribute with it. But now I found a better solution, I wrap it with <pre> tag and it keeps line breaks and tabs.
-
oyvey over 9 yearsBy far the easiest solution on this page.
-
Minh Nguyen almost 9 yearsYou have to use this together with this: .ui-tooltip { white-space: pre-line; }
-
Naidim about 8 yearsGreat if you have 1 newline. For multiples be sure to use global option.
.replace( /\|/g, '<br />' );
-
Olena Vikariy over 7 yearsEasiest solution for me as well. My text already contained \n so I only needed the CSS. Thanks!
-
Matteo Conta about 7 yearsThis is THE answer. Period. Please upvote in order to move this answer on the top.
-
Matteo Conta about 7 yearsPlease look into this page the JDandChips answer, the use of custom characters is unnecessary. You can use html in the title attribute simply changing the function in : return $( this ).prop( 'title');
-
Mike over 6 yearsThe class selector was different for my version of Jquery Tooltip: ` .tooltip-inner { white-space: pre-line; } ` But same principle!
-
huykon225 over 6 yearsthe tooltip will not hide after hover.
-
Wex about 4 yearsYou can also use a regex pattern like :
.replace(/\|/g, '<br />')
. -
Garr Godfrey over 2 yearsthis only replaces the first '|'.