Reputation: 671
I am trying to get the value of a textarea, to check if it's empty, using Javascript and it doesn't work in Opera. In IE, FF and Chrome it works fine, but in Opera 11 and 10 it reports the value to be the empty string, even if it has text. Here's my code:
if (document.getElementById('mytextareaid').value.replace(/(^\s+|\s+$)/, '') == '') {
alert('empty textarea');
}
Using document.getElementById('mytextareaid').innerHTML instead, doesn't work, either. What am I missing?
Upvotes: 0
Views: 1296
Reputation: 6229
Quoting myself from JQuery val() does not work for textarea in Opera :
You may have come across a very obscure bug referred to in a blog post on the Opera sitepatching blog ( http://my.opera.com/sitepatching/blog/facebook-and-some-core-patches ) as "PATCH-287, Hack to make script see typed value in TEXTAREA on blog.ebuddy.com. Opera fails to read correct value from a previously hidden textarea".
I'm a little bit reluctant to recomment workarounds without seeing the full code.
However, when I was looking at this I noticed that setting textarea.contentEditable to something seemed to let me read the value afterwards..it's a weird hack though, and it might cause problems for other browsers.
Upvotes: 0
Reputation: 671
Thank you all for your help. It turns out that it works with a simple page that only has a textarea, but in my particular HTML document it didn't. I finally found a workaround here: JQuery val() does not work for textarea in Opera I don't know what exactly caused the strange behavior, but I do know that the piece of
Upvotes: 1
Reputation: 86386
Replace with this and try
if (document.getElementById('mytextareaid').innerHTML.replace(/(^\s+|\s+$)/, '') == '') {
alert('empty textarea');
}
Upvotes: 3