3

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'); 
})
halfer
  • 19,824
  • 17
  • 99
  • 186
Glenn
  • 111
  • 3
  • 13
  • 3
    Possible duplicate of [How can I use a carriage return in a HTML tooltip?](http://stackoverflow.com/questions/358874/how-can-i-use-a-carriage-return-in-a-html-tooltip) – Anupam May 27 '16 at 06:41
  • no it is not duplicate. I am performing the action from jquery – Glenn May 27 '16 at 06:51
  • If you go through all the solutions in that post you will find [javascript](http://stackoverflow.com/a/13205056/456135) solution there. Anyway approach is same. – Anupam May 27 '16 at 07:08

3 Answers3

7

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
4

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

Vibhor Dube
  • 4,173
  • 1
  • 22
  • 32
0

use <Hr> tag in HTML to set line

here is working example,

   

 var $j = jQuery.noConflict();

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

    $j('#choice31QID405').mouseout(function() { 
    $j(this).removeAttr('title'); 
      $j('#test').html("");
    });
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
      <title>JS Bin</title>
    <body>
      <div id="choice31QID405">Mouse over here</div>
      <div id="test">Tooltip will show up here..</div>
    </body>
Mahendra Kulkarni
  • 1,437
  • 2
  • 26
  • 35
  • dude I cannot add ids to HTML. I am programming on survey platform. The html is generated on the fly. I cannot control it. – Glenn May 27 '16 at 06:54