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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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...

 

... :(

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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"........

Link to comment
Share on other sites

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. :-)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.