This post will discuss how to get the ID of an element in JavaScript and jQuery.

The idea is to use the .attr() or .prop() method to get the id attribute value for the first matched element.

JS


HTML



Edit in JSFiddle

 
Alternatively, you can just access the underlying HTMLInputElement and get its id property:

JS


HTML



Edit in JSFiddle

 
In plain JavaScript, you can do like:

JS


HTML



Edit in JSFiddle

That’s all about getting the ID of an element in JavaScript and jQuery.