I want to write some text to a textbox (using a greasemonkey script to be exact...) but i cant get the right element (textbox) as it somehow doesnt have an id.
This is source that is relevant for the textbox (from original page)

<input name="WebGridBuilder$ctl04$ctl01" type="text" value="something" maxlength="32767" onkeydown="DefaultButton(&quot;SubmitButtonSave&quot;, event);" />

i tried to replace $ with _ in WebGridBuilder$ctl04$ctl01 as it was the case for enabling checkboxes (which had id) but it didnt work

so this is what is my current code:

function add(text){
    var TheTextBox = document.getElementById("WebGridBuilder$ctl04$ctl01");
    TheTextBox.value = TheTextBox.value + rlcmd;

so, now i want to know how can i get the right id or whatever i have to get so i can write to that text box

(im just a beginner, so remember that it isnt shame to ask, but not to know :D)

Thanks in advance!

7 Years
Discussion Span
Last Post by Erco21

Since the input element is not having any id, document.getElementById() wont work except in IE. You can use document.getElementsByName() which returns an array of elements of the given name.

For e.g. if the input element with name 'WebGridBuilder$ctl04$ctl01' is the first element of that name than you can do something like this

function test(){
     var TheTextBox = new Array();
     TheTextBox = document.getElementsByName("WebGridBuilder$ctl04$ctl01");
     TheTextBox[0].value = TheTextBox[0].value + 'test'; //using index 0 assuming it is the first element with the name 'WebGridBuilder$ctl04$ctl01'

Thanks for the help, worked just fine :D

so, if element is second with same name, i would just put 1 instead of 0 index?


so, if element is second with same name, i would just put 1 instead of 0 index?

Yes you are right.

The ideal way is to set an unique id to each HTML element and then use document.getElementById(), since as per the specs the ids must be unique.
Name attribute may not be unique hence document.getElementsByName() return an array of elements and you have to iterate through this array to get the desired element


okay, but i first have to store the actual element first on the page, and then i can store the second one, so if i would just type 1 in array index, i would store the first element in it
i could also do this with ordinary variables, just with different names

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.