How do I get only one DOM element by class name? I am guessing that the syntax of getting elements by class name is getElementsByClassName
, but I am not sure how many elements it's going to return.
-
2It returns an array of DOM elements. You can select the required ones giving the index. Like document.getElementsByClassName ('className') [0] for getting the first element. – Arunkumar Srisailapathi Jan 29 '14 at 16:17
-
@ArunKumar Technically it's a HTMLCollection, which is array-like, not an actual array. – Anthony Grist Jan 29 '14 at 16:19
-
@AnthonyGrist: Yes Agreed! Still you can access them the way I said, can 't you ? – Arunkumar Srisailapathi Jan 29 '14 at 16:20
-
@ArunKumar (i'm just repeating myself now) but it returns a NodeList - not an array. This is important to note because the Array functions are not available for nodeLists. – rlemon Jan 29 '14 at 16:39
-
@rlemon Enlightened :) – Arunkumar Srisailapathi Jan 29 '14 at 16:54
6 Answers
document.getElementsByClassName('className')
would always return multiple elements because conceptually Classes are meant to be applied to multiple elements. If you want only the first element in the DOM with that class, you can select the first element out of the array-like HTMLCollection
returned.
var elements = document.getElementsByClassName('className');
var requiredElement = elements[0];
Else, if you really want to select only one element. Then you need to use 'id' as conceptually it is used as an identifier for unique elements in a Web Page.
// HTML
<div id="myElement"></div>
// JS
var requiredElement = document.getElementById('myElement');

- 53,704
- 14
- 91
- 128

- 4,570
- 5
- 20
- 31
-
1
-
@Ismatjon I believe he was just suggesting the ID approach because it is cleaner overall. however the initial suggestion here holds true. – rlemon Jan 29 '14 at 18:01
-
the initial answer to use `nodelist[index in the node list]` is still correct however. The second part was a suggestion (all I was saying) – rlemon Jan 29 '14 at 18:05
-
1*"Else, if you really want to select only one element. Then you need to use 'id'..."* Not at all. It's perfectly fine to have only a single element with a class. You find it, as some other answers here show, by using `document.querySelector(".the-class-name")`. – T.J. Crowder Mar 20 '22 at 18:45
Clarifications :
getElementsByClassName returns a Node List and not an array
You do not need jQuery
What you were looking for was probably
document.querySelector
:
How do I get only one DOM element by class name?
var firstElementWithClass = document.querySelector('.myclass');
Also see: https://developer.mozilla.org/en/docs/Web/API/Document/querySelector
-
Please may you provide some additional information on why you prefer using this to getElementsByClassName? – GeorgeButter Dec 15 '17 at 04:04
-
6@Buts because querySelector (not querySelectorAll !) stops after the first match. However, querySelectorAll is much slower than getElementsByClassName. – Max Sep 16 '18 at 08:54
-
`document.getElementsByClassName` returns a (live) `HTMLCollection`, not a (static) `NodeList`. – connexo Sep 17 '22 at 07:28
You can use jQuery:
$('.className').eq(index)
Or you can use the javascript built-in method:
var elements = document.getElementsByClassName('className');
This returns a nodeList of elements that you can iterate and make what you want.
If you want only one, you can access the elements in the javascript version like this:
elements[index]

- 441
- 2
- 9
-
3I've made the edit - but be advised you will return a **nodeList** not an array. They are similar, but not the same. – rlemon Jan 29 '14 at 16:34
-
2Also in your jQuery example it is still collecting all of the elements. http://api.jquery.com/eq/ is probably what you want there. – rlemon Jan 29 '14 at 16:35
-
2I know how to use jQuery. this is not what I'm asking. I want a pure javascript approach. – Ismatjon Jan 29 '14 at 17:46
-
1@Ismatjon as I mentioned in the other answer comments, the bracket notation index is going to solve this for you. I caution against using getElementsByClassName if you need to support IE8 (not supported). Instead read about document.querySelector and document.querySelectorAll – rlemon Jan 29 '14 at 18:02
-
@lsmatjon but you can access the first element with this approach above – Lucas Reis Jan 30 '14 at 15:21
-
`document.getElementsByClassName` returns a (live) `HTMLCollection`, not a (static) `NodeList`. – connexo Sep 17 '22 at 07:27
To anyone looking for a one liner
var first = document.getElementsByClassName('test')[0];

- 1,533
- 13
- 18

- 3,811
- 3
- 40
- 50
-
1Your code is using [`HTMLCollection.item()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection/item), not [`NodeList.item()`](https://developer.mozilla.org/en-US/docs/Web/API/NodeList/item). – connexo Sep 17 '22 at 07:31
You can try with this:
document.getElementsByClassName("className")[index]

- 5,489
- 12
- 30
- 42

- 23
- 5