X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

HTML DOM getElementsByTagName() Method

Document Object Reference Document Object

Example

Get all elements with the specified tag name:

document.getElementsByTagName("LI");

Try it yourself »

Definition and Usage

The getElementsByTagName() method returns a collection of all elements in the document with the specified tagname, as a NodeList object.

The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.

Tip: The parametervalue "*" returns all elements in the document.


Browser Support

The numbers in the table specifies the first browser version that fully supports the method.

Method
getElementsByTagName() 1.0 6.0 3.0 3.0 9.5


Syntax

document.getElementsByTagName(tagname)

Parameter Values

Parameter Type Description
tagname String Required. The tagname of the elements you want to get

Technical Details

DOM Version: Core Level 1 Document Object
Return Value: A NodeList object, representing a collection of elements with the specified tagname. The elements in the returned collection are sorted as they appear in the source code.


More Examples

Example

Find out how many <li> elements there are in the document (using the length property of the NodeList object):

document.getElementsByTagName("LI").length;

Try it yourself »

Example

Change the text of the first <p> element (index 0) in the document:

document.getElementsByTagName("P")[0].innerHTML = "Hello World!";

Try it yourself »

Example

Change the background color of all <p> elements in the document:

var x = document.getElementsByTagName("P");
var i;
for (i = 0; i < x.length; i++) {
    x[i].style.backgroundColor = "red";
}

Try it yourself »

Example

Using the "*" parameter.

Get all elements in the document:

document.getElementsByTagName("*");

Try it yourself »


Related Pages

JavaScript Reference: element.getElementsByTagName()


Document Object Reference Document Object


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook