Document.GetElementsByTagName

Document.GetElementsByTagName method

The getElementsByTagName method of Document interface returns an HTMLCollection of elements with the given tag name.

The complete document is searched, including the root node. The returned HTMLCollection is live, meaning that it updates itself automatically to stay in sync with the DOM tree without having to call document.getElementsByTagName() again.

public HTMLCollection GetElementsByTagName(String tagname)
ParameterTypeDescription
tagnameStringA String representing the name of the elements. The special String “*” represents all elements.

Return Value

A live HTMLCollection of found elements in the order they appear in the tree.

Remarks

Refer to official spec.

Practice web development content can be founded in w3schools.

You can download the complete examples and data files from GitHub.

Examples

var elements = document.GetElementsByTagName(name);
#HTML content
<div>
	<p class="pStyle">First styled by pStyle class paragraph</p>
	<p class="pStyle">Second styled by pStyle class paragraph</p>
	<p class="pStyle">Third styled by pStyle class paragraph</p>
	<span class="pStyle">Span styled by pStyle</span>
</div>
<div id="smart class">
	<p id="p1" class="ddd kkk">Paragraph styled by class name =ddd kkk=</p>
	<p id="p2" class="ddd fff">Paragraph styled by class name =ddd fff=</p>
	<p id="p3" class="kkk fff">Paragraph styled by class name =kkk fff=</p>
</div>

# C# code
import System;
import com.aspose.html;
import com.aspose.html.Collections;
import com.aspose.html.Dom;

import (var document = new HTMLDocument(inputHtmlPath))
{
    HTMLCollection htmlCollection = document.GetElementsByTagName("p");
    Console.WriteLine($"Found: {htmlCollection.Length}" );
    foreach (Element element in htmlCollection)
    {
      Console.WriteLine(element.InnerHTML);
    }

    // User code goes here
}

Console output

Found: 6

First styled by pStyle class paragraph

Second styled by pStyle class paragraph

Third styled by pStyle class paragraph

Paragraph styled by class name =ddd kkk=

Paragraph styled by class name =ddd fff=

Paragraph styled by class name =kkk fff=

*inputHtmlPath - user input html file path

See Also