Tony33 Tony33 - 28 days ago 5
Javascript Question

javascript variable not populated correctly

Basically, the "rec" variable is correctly filled, but it's not happening with the "email" variable . Why?

This is my code:

<textarea id="q" name="q"></textarea>
<button onclick='alert(rec);'>
Click
</button>
<a id="email" href="#" target="_blank">Email</a>
<script>
var rec;
var email;

$("#q").keyup(function(){
rec = $('#q').val();
email = 'www.corriere.it/' + rec;
$('email').attr("href",email);
});
</script>


https://jsfiddle.net/wvsc93d4/3/

Answer

You're not using the selector correctly, try:

$('#email').attr("href",email);