views:

242

answers:

8

I am sure my javascript works, because it works with other form elements.

But I cant get it to work with the textarea...

Textarea.value doesnt seem to work... I read someplace textareas dont have values, so how would you solve this.

Im trying to validate a form and checking if value is empty doesnt work.

Thanks

UPDATE:

some code:

                function descriptionValid(fld){
  if (fld.value=="") {
  document.getElementById("annonsera_descriptionempty_error").style.display='block';
  document.getElementById("annonsera_description").focus();
  document.getElementById("annonsera_descriptionshort_error").style.display='none'; 
  return false;
 }
 else if (fld.value.length<3){
  document.getElementById("annonsera_descriptionshort_error").style.display='block';
  document.getElementById("annonsera_description").focus();
  document.getElementById("annonsera_descriptionempty_error").style.display='none'; 
  return false;
 }
  document.getElementById("annonsera_descriptionempty_error").style.display='none'; 
  document.getElementById("annonsera_descriptionshort_error").style.display='none'; 
 return true;
            }

somehow, it doesnt go through the if statements and instead returns true;

+1  A: 
document.getElementById ( "yourtextareaid" ).value;

should return you the value inside the textarea.

rahul
but it doesnt... Hmm...
Camran
Which is your browser?
rahul
firefox is what im using
Camran
I have tested the code in FF 3.5 with no errors.
rahul
the form submits even if the textarea is blank... it doesnt go through the if statements as it should
Camran
A: 

So long as Textarea is a reference to the element, then it should work.

The element doesn't have a value attribute, but it does have a value property.

Whatever it wrong, it isn't in the code you've shown us.

David Dorward
A: 

Textareas have values. To troubleshoot the issue, test first if the textarea element you try to access exists, like this:


alert(document.getElementById("textAreaId")!=null?'exists':'does not exist');

simon
it exists using that code
Camran
A: 

your empty textarea will not return an empty string. try this as your first test;

if (fld && (fld.value === null || fld.value === "")) {

or my simpler favorite is

if (fld && (''+ fld.value === "")) {
Matt Smith
A: 

Thru DOM you can get the value in a Textarea (See here for more info)

Good luck!

ocell
+1  A: 

Just a silly thing incase you've missed it. Textarea treats whitespace as content so if you have your textarea tags like this:

<textarea>

</textarea>

Then the newlines between the tags will be returned as the value. Meaning value == "" will be false. Best to trim the content before you do your test or mean sure your textarea tags have no whitespace in them:

<textarea></textarea>
Pete Duncanson
A: 

One of the first things I would do is stick in some

alert("This part of the if statement fired");

in a few places to find out which bit is actually happening, because I had a similar issue with firefox and discovered that it was actually down to how I set the form to deal with onsubmit events:

<form method="post" action="url" onsubmit="javascript:return checkForm();">

the important bit is the return, without that, Firefox doesn't seem to care about the return value from the function.

MalphasWats
-1 for javascript: in your onsubmit handler. That's totally not right.
Sean McMillan
that's a new one to me! I've not come across that particular Rule of the Internet. Would you mind elaborating so that I can be educated? :)
MalphasWats
A: 

A collegue of mine had a similar problem a while ago. In some browsers the value is never empty. Even if the textarea contains no text, the value contains a line break ending the empty line of text.

You could trim any extra line breaks at the end of the text, and then check if it's empty:

var txt = fld.value.replace(/\\n+$/,'');
if (txt.length == 0) ...
Guffa