200

Using JavaScript, we can get element by id using following syntax:

var x=document.getElementById("by_id");

I tried following to get element by class:

var y=document.getElementByClass("by_class");

But it resulted into error:

getElementByClass is not function

How can I get an element by its class?

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Alpha
  • 13,320
  • 27
  • 96
  • 163
  • http://www.w3schools.com/js/js_htmldom.asp it's mentioned that get element by class name but it's not mentioned how to get element by class(no method mentioned there) Hence I thought, I should try with document.getElementByClass – Alpha Jul 31 '13 at 09:09
  • 15
    be wary of using w3schools.com as a source of information. There are far better places to learn than that. For example, [MDN](https://developer.mozilla.org/en-US/). – Spudley Jul 31 '13 at 09:23
  • I think what happens is that people see the getElementById function and assume there is a class version too. But there isn't because multiple tags can share the same class, but not the same ID. Since classes and IDs are assigned in the tag in a similar looking way (to beginners) it makes them think that they can be used in an interchangeable way. But they shouldn't be, and you should read on the differences and use cases to be sure you are using them properly, as many other developers are going to expect that from you. – osirisgothra Feb 19 '23 at 00:08

4 Answers4

283

The name of the DOM function is actually getElementsByClassName, not getElementByClassName, simply because more than one element on the page can have the same class, hence: Elements.

The return value of this will be a NodeList instance, or a superset of the NodeList (FF, for instance returns an instance of HTMLCollection). At any rate: the return value is an array-like object:

var y = document.getElementsByClassName('foo');
var aNode = y[0];

If, for some reason you need the return object as an array, you can do that easily, because of its magic length property:

var arrFromList = Array.prototype.slice.call(y);
//or as per AntonB's comment:
var arrFromList = [].slice.call(y);

As yckart suggested querySelector('.foo') and querySelectorAll('.foo') would be preferable, though, as they are, indeed, better supported (93.99% vs 87.24%), according to caniuse.com:

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 1
    Thanks. The returned is an array – onmyway133 Jun 15 '17 at 13:00
  • 5
    @onmyway133: NP. Just a side note: the return value is *not* an array, it's a `NodeList` object, which behaves like an array in many ways, but there is a difference. [check the MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/querySelectorAll) for details on the return type, and what its specfics are – Elias Van Ootegem Jun 17 '17 at 22:41
64

Another option is to use querySelector('.foo') or querySelectorAll('.foo') which have broader browser support than getElementsByClassName.

http://caniuse.com/#feat=queryselector

http://caniuse.com/#feat=getelementsbyclassname

Mark Phillip
  • 1,453
  • 17
  • 22
yckart
  • 32,460
  • 9
  • 122
  • 129
35

You need to use the document.getElementsByClassName('class_name');

and dont forget that the returned value is an array of elements so if you want the first one use:

document.getElementsByClassName('class_name')[0]

UPDATE

Now you can use:

document.querySelector(".class_name") to get the first element with the class_name CSS class (null will be returned if non of the elements on the page has this class name)

or document.querySelectorAll(".class_name") to get a NodeList of elements with the class_name css class (empty NodeList will be returned if non of. the elements on the the page has this class name).

udidu
  • 8,269
  • 6
  • 48
  • 68
  • 2
    The return value is a *array-like* `HTMLCollection`, not an `Array` https://dom.spec.whatwg.org/#dom-document-getelementsbyclassname ;) – yckart May 25 '16 at 14:46
10

you can use

getElementsByClassName

suppose you have some elements and applied a class name 'test', so, you can get elements like as following

var tests = document.getElementsByClassName('test');

its returns an instance NodeList, or its superset: HTMLCollection (FF).

Read more

Talha
  • 18,898
  • 8
  • 49
  • 66