jQuery detect if textarea is empty

前端 未结 11 935
抹茶落季
抹茶落季 2020-12-25 11:05

I am trying to determine if a textarea is empty if a user deletes what is already pre-populated in the textarea using jQuery.

Anyway to do this?

This is what

11条回答
  •  滥情空心
    2020-12-25 11:27

    Andy E's answer helped me get the correct way to working for me:

    $.each(["input[type=text][value=]", "textarea"], function (index, element) {
    if (!$(element).val() || !$(element).text()) {
     $(element).css("background-color", "rgba(255,227,3, 0.2)");
    }
    });
    

    This !$(element).val() did not catch an empty textarea for me. but that whole bang (!) thing did work when combined with text.

提交回复
热议问题