views:

66

answers:

4

Simple way to make a preview of a textarea? the code i have below wont format <em>i'm italic!</em> into italics. I'm not very good at js/jquery, and i don't know if the .replace() part is what i need to focus on or what. I've googled it and searched here and couldn't find a simple answer.

$('.comment-block textarea').bind('blur keyup',function() {
    $('#comment-preview').text($('#comment-field').val());
    $('#comment-preview').html($('#comment-preview').html().replace(/\n/g,'<br />'))
});
A: 

You could try:

$('#comment-preview').html($(this).attr("value"));

inside the bind function

Alex
that kinda worked but not reall: it isn't actually styling the comment-preview block, the text i type is up there and any HTML code i have in there is not displayed, but if i code a link in the textarea, it is still a link in the comment-preview but there's no styling, so all of the text looks exactly the same, it doesn't show any text being bold or italic or a link, even though when you click the word that's supposed to look like a link, it works correctly and takes you to the address, but all of the font in the preview looks the same... hmm, any ideas?
android.nick
A: 
$('#comment-preview').html($('#comment-field').val().replace(/\n/g,'<br />')); 

doesnt that cut it?

Stefanvds
wow yeah i guess that's all i needed, but all of the text looks exactly the same, none of it is styles, the link works, but just looking at the preview it doesn't have any other styling than any other text in the preview even though i styled it with css, any ideas?
android.nick
could be helpfull if you post some sample HTML how it really looks, and how your page looks. also, why not use a WYSIWYG editor (google it)
Stefanvds
A: 

When you call $('#comment-preview').text(), it escapes HTML tags. If you make the second line

$('#comment-preview').html($('#comment-field').val());

it will work.

lonesomeday
that kinda worked but not reall: it isn't actually styling the comment-preview block, the text i type is up there and any HTML code i have in there is not displayed, but if i code a link in the textarea, it is still a link in the comment-preview but there's no styling, so all of the text looks exactly the same, it doesn't show any text being bold or italic or a link, even though when you click the word that's supposed to look like a link, it works correctly and takes you to the address, but all of the font in the preview looks the same... hmm, any ideas?
android.nick
A: 
PradeepN
what do you mean?
android.nick