If my textarea is empty do this with jquery
-
28-05-2021 - |
Question
I have a Table,Button and a Textarea, When a user types inside the Textarea and click on the Button the text the user typed gets added to my table as a row. But if the Textarea is empty I dont want it to add empty rows in my table. How can I make it like that?
This is my jquery code:
<script type="text/javascript">
$(document).ready(function () {
$('#CustomButton').click(function () {
$('#CustomPickedTable tbody').append(
$('<tr/>', {
click: function () {
$(this).remove()
},
html: $("<td />", {
html: $("#CustomQuestionTextArea").val(),
'data-attr-id': 5
})
})
);
return false;
});
});
</script
I guess it has to be like " If CustomquestionTextArea is "" return false else " add it to the table".
Thanks in advance!
Solution
if(!$.trim($("#CustomQuestionTextArea").val())) {
return false;
}
OTHER TIPS
You can check length as follows:
if($.trim($("#CustomQuestionTextArea").val()).length > 0) {
$('#CustomPickedTable tbody').append(
$('<tr/>', {
click: function () {
$(this).remove()
},
html: $("<td />", {
html: $("#CustomQuestionTextArea").val(),
'data-attr-id': 5
})
})
// more stuff
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow