Obsivus
Obsivus

Reputation: 8359

If my textarea is empty do this with jquery

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!

Upvotes: 0

Views: 722

Answers (2)

Andreas Wong
Andreas Wong

Reputation: 60516

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
}

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318508

if(!$.trim($("#CustomQuestionTextArea").val())) {
    return false;
}

Upvotes: 4

Related Questions