views:

145

answers:

8

I'm trying to get the value of an input (text/textarea) and document.getElementById('rtext').value returns nothing. If I set a default value (value="XXX"), it returns that value, eve if i edit it.

Here's some sample:

<div class="forms">
    <p><textarea id='rtext'></textarea></p>
</div>

I'm mentioning that the form element <textarea> isn't part of any form and i'm forced to use only document.getElementById. No jQuery and no this.form.Id.value

Please help. THX

+1  A: 

Well, it is document.getElementById(), innit?

Here Be Wolves
sorry, i use the dot(.) I just edited the text... no syntax problem :D
Lucian Vasile
@lucian: still a casing problem, tho
David Hedlund
@lucian: to spell it out, getElementById starts with a lower-case 'g'.
Spudley
ok... thx.. but there's no casing prblem in my code... i just typed bad... sry
Lucian Vasile
@Lucian: do you have another element with the same ID on the page?
Andy E
nope... I'm not a begginer... I've confrunted with the same problem before but I solved it out with this.form.Id.value or jQuery. But this time i can't use them... im ust use a javascript function that uses getElementById an I'm not allowed to modify it. It's very frustrating bcz I usually solve my problems.
Lucian Vasile
@Lucian Vasile - No one is saying you're a beginner, but you've provided very little information for what really is a JavaScript 101 problem, so if people are throwing guesses out it's because you haven't provided enough information for anyone to help you troubleshoot. It might also help if you stopped using abbreviations and paid a little more attention to your grammar and casing in written language. It suggests that you likely have a similar problem in your code.
Joel Etherton
+5  A: 

getElementById() (case is important) is a member of the document object. To call the function, you need to refer to the object and then the function. In dot notation, it should be:

document.getElementById()

document.getElementById() - MDC

Note that IDs must be unique on the page and having another element with the same ID might be causing your issue.

Andy E
it is unique...
Lucian Vasile
@Lucain well then you'll have to post your javascript snippets for us to be any more helpful
Here Be Wolves
+1  A: 

Works for me:

   <div class="forms">
       <p><textarea id='rtext'></textarea></p>
   </div>

   <input type    = "button" 
          onclick = "alert(document.getElementById('rtext').value);" 
          value   = "run" />

   <a     href    = "javascript:void(0);"
          onclick = "alert(document.getElementById('rtext').value)"
         >GET</a>

Things to consider:

  • Make sure the code is being called; there may be errors in your JavaScript
  • Make sure the id is unique to the HTML/DOM as Andy E has pointed out, though I think the function will grab the first object it finds, if there are multiple.
  • Make sure the spelling/case is correct; getElementById() and getElementByID() are not the same function
  • Make sure you're trying to grab the value after it is populated using the correct event (onkeyup vs onkeypress)
vol7ron
the event takes place 'onclick' in <a href="javascript" onclick="alert(document.getElementById('rname').value)">GET</a>
Lucian Vasile
Are you getting `rname` or `rtext`? I think you should post your full code on [pastebin](http://www.pastebin.com)
vol7ron
A: 

Since you've tried everyone's solutions and you can't find the error I would suggest that you try debugging the JavaScript in your browser.

In IE8 (I don't remember if 7 had it) hit F12 and bring up the developer toolbar. On the script tab you can test your javascript by hand in the console or debug it.

In FireFox use Firebug.

In Chrome hit Ctrl+Shift+J, the console will be at the bottom of the screen. You can also debug the scripts on the scripts tab.

confusedGeek
yeah... I've tried that... no javascript errors
Lucian Vasile
+1  A: 

Try to put your code snippet at the end of your <body> tag. Your javascript is probably executing before your <textarea> element has loaded.

So:

<html>
<body>
 <div class="forms">
  <p><textarea id='rtext'></textarea></p>
 </div>
<script type="text/javascript">
  var el = document.getElementById('rtext');
  alert(el);
</script>
</body>
</html>

(I've left out the head and other tags to keep it compact :)

Kevin
A: 

If you are using the Text element within an Update Panel then the Id being rendered on the page is no longer rtext .Only if you are using an Update Panel. DO a view source code to verify if the ID is retained as rtext

Rudra
Who said anything about an UpdatePanel? Nothing tagged here indicates this is a .Net problem.
Joel Etherton
A: 

ok... i solved the problem:

I inserted the form tags, I modified the

<a href="javascript" onclick="alert(document.getElementById('rname').value)">GET</a>

with

<input type="button" onclick="document.getElementById('rname').value=this.form.rname.value" />

it works fine... thx anyway for troubling and sry if I didn't give enough details.

Lucian Vasile
A: 

Have you tried:

document.getElementById('rtext').textContent
Mike Ratcliffe