Reputation: 502
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
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
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
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
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:
getElementById()
and getElementByID()
are not the same functiononkeyup
vs onkeypress
)Upvotes: 1
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
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