I am trying to get the updated value of the input field. actually i am updating the value using keyup function of jquery. and trying to get it on button click. but not able to update please help.
$('#simple').keyup(function() {
var keyed = $(this).val();
$("#code").val(keyed);
});
var htmlString = $('#code').val();
$(document).ready(function() {
$("#snippet").click(function(){
alert(htmlString);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="simple"></textarea>
<input type="hidden" id="code" value="">
<button id="snippet">Click</button>
var htmlString = $('#code').val();is in global scope already evaluated at the beginning as empty. By the time you use it, its using the already empty value. Change scope or time of value invocation