Search code examples
jqueryhtmlappendline-breaks

How to pass line breaks through jQ.append()?


Using some simple jQ logic from http://www.davidjrush.com/blog/2011/12/simple-jquery-tooltip/ to create a simple tool tip like effect.

However when I add a line break in the JQ code everything goes south and the logic stops working (FF).

Works:

$(this).append('<div class="tooltip"><p>This is a tooltip. It is typically used to explain something to a user without taking up space on the page.</p></div>');

Does Not:

$(this).append('
<div class="tooltip"><p>This is a tooltip. It is typically used to explain something to a user without taking up space on the page.</p></div>
');

Checking the jQ docs it states nothing I could find about this being an issue.

Anyone know why it breaks and / or how I pass line breaks to jQ.append()


Solution

  • inside the append you have to skip the line breaks with backslash.

    in your case:

    $(this).append('\
    <div class="tooltip"><p>This is a tooltip. It is typically used to explain something to a user without taking up space on the page.</p></div>\
    ');