admin管理员组文章数量:1332873
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.
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.
- Can you post the rest of the first line? What exactly is "<textarea> value"? – casablanca Commented Nov 6, 2010 at 0:50
- Ok, there you go. Hope it helps – chromedude Commented Nov 6, 2010 at 0:53
4 Answers
Reset to default 3If the <textarea>
element actually exists, value
will be ""
(empty string) if there is no text in it. From your ment on the other answer, "cannot read property of null" means document.getElementById('textLoc')
is returning null
: make sure that the ID of the element is correct.
Javascript is quite flexible. Try:
if (text) {
...
}
You're missing a space before the id
attribute in your HTML, which is why id
is not being set, and hence you can't get an element by that ID.
If you're getting "Cannot read property 'value' of null", then that means there's no element with that ID (i.e., getElementById returned null). If a textarea actually exists, the value is ''
when empty. To safeguard against errors where you access a property of null:
var textarea = document.getElementById('textLoc');
if (textarea && textarea.value) ...
This code:
<textarea rows="10" style="display: block;"id="textLoc"
placeholder="Text to test"cols="50">
is not correct.
The tag should be closed:
<textarea rows="10" style="display: block;"id="textLoc"
placeholder="Text to test"cols="50"/>
本文标签: javascriptWhat is the value of a lttextareagt if there is no text in itStack Overflow
版权声明:本文标题:javascript - What is the value of a <textarea> if there is no text in it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742304946a2449727.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论