justasd
justasd

Reputation: 401

JavaScript: document.all.item() replacement

I am using document.all.item("name") and it works in IE 10 and Google Chrome 29 but not in Firefox. Is there a replacement that is compatible with Firefox?

Upvotes: 2

Views: 8327

Answers (3)

bp4D
bp4D

Reputation: 961

I know this is pretty old thread which I happened to stumble upon today. A fact we need to consider in replacing document.all.item with document.getElementByName is that former returns HTMLCollection while latter returns NodeList. Here is another SO thread discussing the differences between these two.

Upvotes: 0

Dinesh Rajan
Dinesh Rajan

Reputation: 2584

function findelement(name) {
  if (document.getElementsByName(name))
    return document.getElementsByName(name) ;
  else
    return document.getElementById(name) ;
}

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324780

document.getElementsByName("name") should do the same thing, but better because it handles the case where there are multiple elements with the same name properly (ie. radio buttons, form arrays, etc.)

Upvotes: 1

Related Questions