Lucian Vasile
Lucian Vasile

Reputation: 502

document.GetElementById doesn't work in any kind of browser

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

Upvotes: 2

Views: 8518

Answers (8)

Lucian Vasile
Lucian Vasile

Reputation: 502

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

Upvotes: 1

Mike Ratcliffe
Mike Ratcliffe

Reputation: 29

Have you tried:

document.getElementById('rtext').textContent

Upvotes: 1

Rudra
Rudra

Reputation: 1

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

Upvotes: 0

Kevin
Kevin

Reputation: 5694

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

Upvotes: 1

vol7ron
vol7ron

Reputation: 42099

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)

Upvotes: 1

Ken Henderson
Ken Henderson

Reputation: 2828

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.

Upvotes: 0

Andy E
Andy E

Reputation: 344567

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.

Upvotes: 5

jrharshath
jrharshath

Reputation: 26583

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

Upvotes: 2

Related Questions