Get value from text area
Use .val()
to get value of textarea and use $.trim()
to empty spaces.
$(document).ready(function () {
if ($.trim($("textarea").val()) != "") {
alert($("textarea").val());
}
});
Or, Here's what I would do for clean code,
$(document).ready(function () {
var val = $.trim($("textarea").val());
if (val != "") {
alert(val);
}
});
Demo: http://jsfiddle.net/jVUsZ/
$('textarea').val();
textarea.value
would be pure JavaScript, but here you're trying to use JavaScript as a not-valid jQuery method (.value
).
Vanilla JS
document.getElementById("textareaID").value
jQuery
$("#textareaID").val()
Cannot do the other way round (it's always good to know what you're doing)
document.getElementById("textareaID").value() // --> TypeError: Property 'value' of object #<HTMLTextAreaElement> is not a function
jQuery:
$("#textareaID").value // --> undefined