I have a textarea. I need to update text in a div when a value in textarea is changed by either typing in it or by placing some value in it via another jQuery function or pasted.
$(document).ready(function(){
function myFunc() {
var input = $("#myTxt").val();
$("#txtHere").text(input);
}
myFunc();
// EDIT BELOW -- this will update while typing
$("#myTxt").keyup(myFunc);
});
<textarea id="myTxt"></textarea>
<div id="txtHere"></div>
It loads the value on page load but I'm not sure what to use to check for value in the textarea...
You can simply use the val() method to set the value of a textarea dynamically using jQuery.
Alternatively, you can use jQuery's . html() method, which uses the browser's innerHTML property to replace textarea content with the new content completely. Another good solution is to use the . val() method to set the text value of the textarea element.
<textarea> does not support the value attribute.
The value property sets or returns the contents of a text area. Note: The value of a text area is the text between the <textarea> and </textarea> tags.
$(document).ready(function(){
function myFunc(){
var input = $("#myTxt").val();
$("#txtHere").text(input);
}
myFunc();
//either this
$('#myTxt').keyup(function(){
$('#txtHere').html($(this).val());
});
//or this
$('#myTxt').keyup(function(){
myFunc();
});
//and this for good measure
$('#myTxt').change(function(){
myFunc(); //or direct assignment $('#txtHere').html($(this).val());
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With