开发者

Javascript text field live view

I am trying to make a similar bit of code like at the bottom of this page to leave a comment. I have the basic code but the output does not register new lines (or HTML, but that isn't important). I have the function below called on key-up on the text field. Any help would be greatly appreciated. Thanks

Here is the whole page (Now working)

<html>
<body>

<form>
    <textarea id="text" onkeyup="outputText()"></textarea>
</form>

<div id="outputtext" style="width:500px;">
</div>

</body>

<script type="text/javascript">
    function outputText()
    {
        var text = document.getElementById('text').innerHTML;
       开发者_运维百科 document.getElementById('outputtext').innerHTML = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2');
    }
</script>

</html>


document.getElementById('outputtext').innerHTML = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2')


Have you tried getting the textarea contents as

var text = document.getElementById('text').value; instead?


I think it's good for you to take a look at how tools like jQuery can make your live easier in this kind of cases. Your particular question is a bit unclear however...can you give us more details?


You can use the <pre> (preformatted) tag so that html and carriage returns are represented without doctoring the field input

html:

<input id="text" type="text" />
<pre id="outputtext"></pre>

jQuery:

$(document).ready(function () {
    $('#text').keyup(function () {
        $('#outputtext').html($(this).val());
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜