-2

I'm converting some VBS to JS and have come across a line I'm unsure about:

document.all.item(strCheckboxName,i)

As there is no constructor taking 2 arguments for document.getElementById(), I have no clue of what this instruction was meant to do.

In other words, what is the purpose of the second argument i here ?

document.all.item(strCheckboxName, i)
Stphane
  • 3,368
  • 5
  • 32
  • 47
Milnelli
  • 55
  • 7
  • In JS, the argument to `.item` *should* be the index you want to look up, and nothing else. `document.all.item(i)` might work - or `document.all[i]`, or, even better, avoid `document.all` altogether – CertainPerformance Oct 13 '19 at 04:53
  • My question is: what is the purpose of the second parameter with: document.all.item(strCheckboxName,i) ? – Milnelli Oct 13 '19 at 05:05
  • In Javascript, there isn't any - only the first argument matters AFAIK – CertainPerformance Oct 13 '19 at 05:06
  • Lol.. yes I understand that.. but if I have to convert document.all.item(strCheckboxName,i) then I have to understand what it's doing first. – Milnelli Oct 13 '19 at 05:17
  • 2
    There's not enough context to say - there's nothing in the question that explains what your `strCheckboxName` or `i` is, or what exactly you're trying to select. The best solution would be to replace it completely with `querySelector` if possible – CertainPerformance Oct 13 '19 at 05:20
  • Lol omg.. x = document.all.item(strValue1) will return/set x to the control 'strValue1'.. but what does the second parameter do? Do you know? – Milnelli Oct 13 '19 at 05:22
  • 1
    Like I already said, the second parameter doesn't do anything in Javascript, see [docs](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection/item) – CertainPerformance Oct 13 '19 at 05:24
  • Am I being trolled, lol, what does the second parameter do in VBS? – Milnelli Oct 13 '19 at 05:26

1 Answers1

1

item method (object.item(name, index)) takes the following parameters:

name [in]

Specifies the object or collection to retrieve. If this parameter is an integer, it is the zero-based index of the object. If this parameter is a string, all objects with matching attribute name or id properties are retrieved, and a collection is returned if more than one match is made.

index [in, optional]

Specifies the zero-based index of the object to retrieve when a collection is returned.

https://learn.microsoft.com/en-us/previous-versions//hh870051(v=vs.85)


What this means to you is that if strCheckboxName refers to a control array, you would use the second parameter to specify which object in the array. It's an optional parameter.

Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29