Question #505

In JavaScript, how to select element by class?

Answer

In JavaScript, the getElementsByClassName(className) method returns an array-like object of all child elements which have the given class name. Here are some examples:

// Get all elements from the document that have the class 'center'
document.getElementsByClassName('center')

// Get all elements that have a class of 'bold', inside of an element with ID 'container'
document.getElementById('container').getElementsByClassName('bold')

4 events in history
Answer by Alphonsio 12/17/2020 at 06:18:49 PM

In JavaScript, the getElementsByClassName(className) method returns an array-like object of all child elements which have the given class name. Here are some examples:

// Get all elements from the document that have the class 'center'
document.getElementsByClassName('center')

// Get all elements that have a class of 'bold', inside of an element with ID 'container'
document.getElementById('container').getElementsByClassName('bold')

Answer by Alphonsio 12/17/2020 at 06:18:29 PM

In JavaScript, the getElementsByClassName(className) method returns an array-like object of all child elements which have the given class name. Here are some examples:

// Get all elements from the document that have the class center
document.getElementsByClassName('center')

// Get all elements that have a class of 'bold', inside of an element with ID 'container'
document.getElementById('container').getElementsByClassName('bold')

Answer by Alphonsio 12/17/2020 at 06:17:46 PM

In JavaScript, the getElementsByClassName(className) method returns an array-like object of all child elements which have the given class name. Here are some examples:

// Get all elements from the document that have the class center
document.getElementsByClassName('center')

// Get all elements that have a class of 'bold', inside of an element with ID 'container'
document.getElementById('container').getElementsByClassName('bold')



Question by Alphonsio 12/17/2020 at 06:13:19 PM
In JavaScript, how to select element by class?
# ID Query URL Count

Icons proudly provided by Friconix.