Search code examples
htmljqueryonkeyup

Show text from textarea in a div JS


As in title, I would like to see what I'm writing by input text.

There is my code:

function setLogo(txtLogo){
    //edit logo by textarea
    var myLogo = $("#title-logo");
    myLogo.text(txtLogo);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="txtLogo" onkeyup="setLogo(this)" value="Logo Text">
<h2 id="title-logo"></h2>

When I'm writing I can see [object HTMLInputElement] inside h2 but if I'm not writing nothing I can't see nothing on it. Any ideas?


Solution

  • You don't have to set the object txtLogo as text, but instead its value.

    function setLogo(txtLogo){
        //edit logo by textarea
        var myLogo = $("#title-logo");
        myLogo.text(txtLogo.value);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="text" id="txtLogo" onkeyup="setLogo(this)" value="Logo Text">
    <h2 id="title-logo"></h2>