6

Possible Duplicate:
Get list of all input objects using JavaScript, without accessing a form object

How can I get elements, like all buttons on a page, without class or ID?

Is there anything like this?

document.getElementsByType(button)

EDIT

My confusion was how to get all buttons (not just one) without class or ID.

the solution ended up being

    var buttons = document.getElementsByTagName('button');

for (var i = 0, len = buttons.length; i < len; ++i) {

but your answer is what led me to find out what I didn't know to ask in the first place... So, thanks everyone!

Community
  • 1
  • 1
  • 5
    document.getElementsByTagName('button') you mean? – StiveKnx Mar 08 '12 at 19:40
  • @StiveKnx, seriously, if this isn't a duplicate you should have just answered with that comment (albeit with some explanation, maybe, and/or links to references). – David Thomas Mar 08 '12 at 19:41
  • 1
    @David Thomas, Sorry, but I wasn't sure if is really that what he wanted, maybe he wants to get all elements which type is "button", example: `` – StiveKnx Mar 08 '12 at 19:43
  • 1
    @StiveKnx he will get all elements which type is "button" with document.getElementsByTagName('button')... – mothmonsterman Mar 08 '12 at 19:46
  • @happytimeharry: How is `getElementsByTagName('button')` going to fetch an element that has the tagName `input`? –  Mar 08 '12 at 19:51
  • 1
    @am not i am - well, if you are like the OP and looking to get _button_ (TAGS) on a page, then that's what you should use. – thescientist Mar 08 '12 at 20:05
  • @thescientist: Where does OP state all button TAGS? There are different types of buttons. *StiveKnx* gave one example. *happytime harry* suggested that `gEBTN` would fetch those as well. I asked how that's possible when the example given has a different *tagName*. I've still received no answer. –  Mar 08 '12 at 20:10
  • "How can I get elements, like all buttons on a page, without class or ID?". I took that to mean – thescientist Mar 08 '12 at 21:02

2 Answers2

20

document.getElementsByTagName

document.getElementsByTagName('button')
thescientist
  • 2,906
  • 1
  • 20
  • 15
4

There's getElementsByTagName, querySelector, and querySelectorAll, among others. Take your pick!

Brandan
  • 14,735
  • 3
  • 56
  • 71