views:

75

answers:

4

Hello, how do i do like Stackoverflow, when you type in this field, that the it previews directly under it?.. I have no idea how this have been made and i really like to do something like this to my website. I have a textarea that you edit, and then..? how to?

Thanks

+1  A: 

Also take a look on their blog

http://meta.stackoverflow.com/search?q=wmd

http://wmd-editor.com/

Michael B.
+1  A: 

Simple, place a div element and then on the onMouseup of the text box you can have a jquery event move the info

<textarea id="entry" onmouseup="$('#output').html($('#entry').val())"></textarea>
<div id="output"></div>
Mech Software
This approach would create lots of problem with page rendering unless you also HTMLEncode the data. http://www.yuki-onna.co.uk/html/encode.html Imagine if you typed in <div>, the absense of a closing DIV may throw the page layout completely out. That's not even mentioning the implications of SCRIPT tags unless you HTML encode.
Nissan Fan
and voila... you have just enabled XSS attack vectors on your site ;)
Gaby
He didn't say anything about how he was saving them :P
Adam Bard
+1  A: 

Use JQuery's keypress event:

$('#in').keypress(function() {
  $('#out').text($(this).val());
});
Mark Kinsella
+1  A: 

Using jquery, in the header in a script tag:

    $(document).ready(function(){
        $('#textbox-id').keypress(function(){
          $('#preview-id').html($(this).val());
        }
    });

Then, in your html:

<textarea id="textbox-id"></textarea>
<div id="preview-id"></div>

Edit

Damn, beaten by a few seconds. Also, wmdeditor is exactly it.

Adam Bard
This does not work.. I get nothing output in the <div> .. I have it like this: <form action="javascript:profilinsert()" method="get"> <textarea name="profiltekst" id="profiltekst" rows="17" cols="70" style="border-style: solid; border-width: 1px"><? echo $p["profiltekst"]; ?></textarea><br> <input type="submit" name="Submit" value="Opdatér"/> </form> <div id="insert_response"></div> <div id="preview"></div>And then ofcourse the script in <head + script tag..
Karem