Search code examples
onkeyup

not able to update variable onkeyup javascript


I have a textarea and i want to update a javascript variable onkeyup for textarea.

Here is my code:

 <textarea id="q" name="q"></textarea>
 <button onclick='alert(rec);'>
 Click
 </button>
<script>
var rec;

$("#q").keyup(function(){
      rec = document.getElementById("q").value;
});
</script>

i've implemented jquery , of course.

https://jsfiddle.net/wvsc93d4/


Solution

  • Try like this:

    $("#q").keyup(function(){
          rec = $('#q').val();
    });
    

    See it here: https://jsfiddle.net/ddan/wvsc93d4/1/