0

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/

2
  • Actually your should also work, just set in JSFiddle to load JQuery lib as you are using it, and to append the script to the body element. Commented Nov 9, 2016 at 1:30
  • Here is yours without code change, just included jquery: jsfiddle.net/ddan/wvsc93d4/2 Commented Nov 9, 2016 at 1:33

2 Answers 2

1

Try like this:

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Can you tell me why with the same structure, this does not work? jsfiddle.net/wvsc93d4/3
0

Give this a try. You were just trying to call rec before it was set.

var rec = "";

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

$("#showMe").on("click", function(){
console.log(rec);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<textarea id="q" name="q"></textarea>
 <button id="showMe">
 Click
 </button>

3 Comments

Can you tell me why with the same structure, this does not work? jsfiddle.net/wvsc93d4/3
you forgot to put the # in front of the $("email").attr("href",email); Should be $("#email).attr("href", email);
also, you might want to add the http:// with your www.corriere.it link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.