dangerChihuahua007
dangerChihuahua007

Reputation: 20895

How do I reliably check if a textarea element has any inner text in jQuery?

Apparently,

if (!($('#myTextArea').html()))
    alert("Textarea empty");

doesn't work.

Upvotes: 0

Views: 509

Answers (3)

Francis Lewis
Francis Lewis

Reputation: 8980

If you want to check for html, it's best to check it's length:

if (!$('#myTextArea').html().length)

However, since a textarea is a form element, it will have a value, so you should always use use .val:

if (!$('#myTextArea').val())
    alert('Textarea empty');

Upvotes: 1

Tim Down
Tim Down

Reputation: 324587

Always use val() or the plain DOM value property. Any other property such as innerText, textContent or innerHTML (as used by jQuery's html() method) will not update to reflect the current value of of the textarea.

For your example:

if ($('#myTextArea').val() == "") {
    alert("Textarea empty");
}

Upvotes: 1

COLD TOLD
COLD TOLD

Reputation: 13579

try those maybe it helps

if (($('#myTextArea').html()==""))

    alert("Textarea empty");


if (($('#myTextArea').text()==""))

    alert("Textarea empty");

Upvotes: 1

Related Questions