Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add line break in title attribute using jquery

Please find my code below which adds a tooltip on mouseover event to a field in my survey engine. What I want to achieve is add line breaks to the tooltip. Any help is greatly appreciated.

var $j = jQuery.noConflict();

$j('#choice31QID405').mouseover(function() { 
$j(this).attr('title','My name is Glenn. <Add a line break>. I am a good boy'. <Add a line break>. I live in New Delhi); 
})

$j('#choice31QID405').mouseout(function() { 
$j(this).removeAttr('title'); 
})
like image 332
Glenn Avatar asked Oct 15 '25 08:10

Glenn


2 Answers

On modern browsers, you can just use a line break:

$("#target").attr("title", "Hello\nWorld");
<p title="Hello
World">
This one is hardcoded in the HTML.
</p>
<p id="target">
This one is added later
</p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

That works fine on current (as of this writing) Chrome and Firefox, as well as IE11.

like image 165
T.J. Crowder Avatar answered Oct 17 '25 01:10

T.J. Crowder


Use entity code &#010; for line break. Your code will look something like this:

$j(this).attr('title','My name is Glenn.&#010;I am a good boy'.&#010;I live in New Delhi);

Refer this FIDDLE

like image 20
Vibhor Dube Avatar answered Oct 16 '25 23:10

Vibhor Dube



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!