JavaScript Element id Property Tutorial

In this section, we will learn what the Element id property is and how to use it in JavaScript.

What is Element id Property in JavaScript?

The JavaScript Element id property is used to set or get the value of the id attribute of an element.

Element id Property Syntax:

element.id;

element.id = “newValue”;

Element id Property Parameter

The value we set for this property will be considered as the value of the id attribute for the target element.

Element id Property Return Value

The return value of this property is the current value assigned to the id attribute.

Note: if the element does not have the id attribute, the return value of this method becomes an empty string.

Example: JavaScript get id of element

See the Pen JavaScript get id of element by Omid Dehghan (@odchan1) on CodePen.

Example: add id to element JavaScript

See the Pen add id to element JavaScript by Omid Dehghan (@odchan1) on CodePen.

Leave a Reply