Document.GetElementById

Document.GetElementById method

The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they’re a useful way to get access to a specific element quickly.

If you need to get access to an element which doesn’t have an ID, you can use querySelector() to find the element using any selector.

public Element GetElementById(string elementId)
ParameterTypeDescription
elementIdStringThe ID of the element to locate. The ID is case-sensitive string which is unique within the document; only one element may have any given ID.

Return Value

An Element object describing the DOM element object matching the specified ID, or null if no matching element was found in the document.

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

// HTML content
<div id="uniqueIdentifier">Container with ID - identifier</div>

// C# code
using System;
using Aspose.Html;
using Aspose.Html.Dom;
...
	using (var document = new HTMLDocument(inputHtmlPath))
		{
			Element element = document.GetElementById("uniqueIdentifier");
			HTMLDivElement divElement = (HTMLDivElement) element;
			Console.WriteLine(divElement.InnerHTML);

			// User code goes here
   }

// Console output

Container with ID - identifier

*inputHtmlPath - user input html file path

See Also