I have to enter a random value in a textarea
of a web site by using grease monkey script the text area didn\'t have an id
so document.getElementB
Use getElementsByName:
document.getElementsByName("inpx_msg")[0]
Obviously, you'll have to check if it exists, etc.
It might be helpful to use getElementsByTagName:
document.getElementsByTagName("textarea")[0]
Of course, if you have more than one textarea tag, you will have to distinguish between them based on properties they might have. Or, if you know your textarea resides inside a specific element which has an ID, you could try
document.getElementById("someparentid").getElementsByTagName("textarea")[0]
You could use element.querySelector if you want to be able to specifically target an element using a CSS selector, similar to this:
var textArea = document.querySelector('table tr td.pd5 textarea.msgbox');
textArea.value = "My Random Text";
The above is working on the following HTML example:
<table>
<tbody>
<tr>
<td class="tdv pd5"><span class="tbg">Message</span> </span>
</td>
<td class="pd5">
<textarea name="inpx_msg" class="msgbox"></textarea>
<br />
<div class="tsm">140 characters max.</div>
</td>
</tr>
</tbody>
</table>
DEMO - Using element.querySelector
Off course, you can now change the CSS selector to anything you like to match your DOM hierarchy. There is lots of flexibility using element.querySelector
.