I want to show an alert when a certain function is run and an if statement in that function finds that the <textarea> has no text in it. I tried:
<textarea rows="10" style="display: block;"id="textLoc" placeholder="Text to test"cols="50"></textarea>
text = document.getElementById('textLoc').value;
if (text == "") {
//show alert
}
but it did not work. Any ideas?
Update: I tried printing the value of the <textarea> in an alert and it showed the value as undefined. I then tried typeof text == "undefined" and it did not work either.
If the
<textarea>element actually exists,valuewill be""(empty string) if there is no text in it. From your comment on the other answer, “cannot read property of null” meansdocument.getElementById('textLoc')is returningnull: make sure that the ID of the element is correct.