Jump to content

[SOLVED] Getting text from a <textarea> with document.getElementById is possible or not?


ltrem

Recommended Posts

Hey guys,

 

I just CAN'T get the text from a <textarea> with the getElementById... the only way I can do it is by putting my <textarea> in a <form> and doing something like

document.form1.textarea1.value

 

why is this not working? -->

 

document.getElementById('textarea1').value

 

Thanks

It works for me:

 

<html>
<head>
   <title>Blah</title>
   <script type="text/javascript">
      window.onload = function(){
         var oTextarea = document.getElementById('myText');
         
         alert(oTextarea.value);
      }
   </script>
</head>

<body>
   <textarea id="myText">Hello world</textarea>
</body>

</html>

 

How are you filling the textarea?

I have this when the page load

 

<textarea name="commentaire" id="commentaire" rows="3" cols="54"></textarea>

 

Then I type some text, click on a button that execute a alert and the alert doesn't display anything...

 

if I put this otherwise

 

<textarea name="commentaire" id="commentaire" rows="3" cols="54">TEST</textarea>

 

and click my button, the alert display TEST...

 

... :(

The following works for me in IE8 and FF 3.5:

 

<html>
<head>
   <title>Blah</title>
   <script type="text/javascript">
      window.onload = function(){
         var oTextarea = document.getElementById('myText');
         var oButton = document.getElementById('myButton');

         oButton.onclick = function(){
            alert(oTextarea.value);
         }
      }
   </script>
</head>

<body>
   <textarea id="myText"></textarea>
   <button id="myButton">Click</button>
</body>

</html>

For fun I tried to copy past your textarea and javascript code and it worked...

 

I then tried my code again.. didn't work... I then changed the id="commentaire" to id="whatever" and it worked..... WTF  :wtf: :wtf: :wtf:

 

It seems that the code just don't like the id "commentaire".... WTF !????

 

I searched all my code and I have NO other place with "commentaire"........

For fun I tried to copy past your textarea and javascript code and it worked...

 

I then tried my code again.. didn't work... I then changed the id="commentaire" to id="whatever" and it worked..... WTF  :wtf: :wtf: :wtf:

 

It seems that the code just don't like the id "commentaire".... WTF !????

 

I searched all my code and I have NO other place with "commentaire"........

 

I'm just going to say that it most likely is a typo... however, if you post your code, it usually helps to have a second set of eyes. :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.