2

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?

justasd
  • 401
  • 1
  • 12
  • 26

3 Answers3

1

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.)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • `.item` also finds based on id AFAIK, so the replacement depends on how it's actually being used, right? – Jon Sep 27 '13 at 09:20
  • @Jon I thougt that too but it does seem to find elements by name as well, for example `document.all.item('q')` finds the search `` on this page – andyb Sep 27 '13 at 09:27
  • @andyb: That's the whole point: it finds both by id *and* name. `getElementsByName` does not. – Jon Sep 27 '13 at 09:29
0
function findelement(name) {
  if (document.getElementsByName(name))
    return document.getElementsByName(name) ;
  else
    return document.getElementById(name) ;
}
Dinesh Rajan
  • 2,376
  • 23
  • 19
0

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.

bp4D
  • 915
  • 12
  • 20