htmlformsgetelementbyidgetelementsbyname

Access Form elements with "name" when no "ID" available


I'm new to scripting. I am trying to automate some web form filling tasks.

I was able to do most parts where there was as HTML "ID" tag assigned.

For instance,

If the form has this:

<input type="text" maxlength="30" size="30" value=" " name="desc" id ="id1"></input>

I used something like below code to assign a value to the Textbox.

oIE.Document.All.Item("id1").Value = "MESSIAH1"

However, there are some forms that I am handling that have (code below):

<input type="text" maxlength="30" size="30" value=" " name="desc"></input>

No "Id", just "name"

What do we do here? Any pointers? In general, my question is how would I fill / access forms (elements) when I don't have an associated HTML "ID" , but just a "name".

Appreciate all help

*If it matters, I'm running VBSCRIPT on IE8.


Solution

  • getElementsByName returns an HTMLCollection. You can access the value of the first item like this:

    document.getElementsByName("desc").item(0).value
    

    Or like this:

    document.getElementsByName("desc")[0].value
    

    More info: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection

    Font: Using document.getElementsByName() isn't working?