Q:

Auto update div content while typing in textarea using jQuery

0

Auto update div content while typing in textarea using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the jQuery keyup() method

You can use the jQuery keyup() method in combination with the val() and text() method to simultaneously update the <div> element content, while user writing the text in a <textarea>.

Let's try out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Auto Update Div with Content from Textarea</title>
<style>
    .output{
        padding: 10px;
        min-height: 50px;
        border: 1px solid #e4e4e4;
    }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("#myTextarea").keyup(function(){
        // Getting the current value of textarea
        var currentText = $(this).val();
        
        // Setting the Div content
        $(".output").text(currentText);
    });
});
</script>
</head>
<body>
    <form>
        <textarea id="myTextarea" rows="5" cols="60" placeholder="Type something here..."></textarea>
    </form>
    <div class="output"></div>
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to insert HTML content into an iFrame using jQ... >>
<< How to get selected file name from input type file...