views:

247

answers:

3

I have a simple jquery snippet here:

$("#someid").html("Text Here");

The only problem I am having is that I am putting Database data inside the html().

$("#someid").html("<?php echo $row['tablecolumn']; ?>");

It works great! Except for one thing.
It does not show when I have 'breaks' in the text from the database.

I'm sure I need to do some sort of escaping of characters. But I don't know quiet what that is. Any ideas out there?

+1  A: 

Use nl2br to add HTML line breaks to the physical ones and encode the data with json_encode:

$("#someid").html(<?php echo json_encode(nl2br($row['tablecolumn'])); ?>);
Gumbo
A: 

So, your text does not show up with line breaks as you'd expect?

Perhaps you need to convert the line breaks to
tags on the server side before sending the data over. If you're using PHP for instance, you could use the nl2br function for to create HTML line breaks anywhere there is a \n in the text.

Alex JL
I was first applying the nl2br function, and got the same result, nothing returned.Gumbo was right about his entry:$("#someid").html(<?php echo json_encode(nl2br($row['tablecolumn'])); ?>);that worked great!
A: 

Use .text() instead of .html().

From jQuery Attributes/text

Set the text contents of all matched elements. Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities). Cannot be used on input elements. For input field text use the val attribute.

ahsteele