get an element’s id

get an element’s id

Is there another way to get an DOM element’s ID?


Solution 1:

Yes you can just use the .id property of the dom element, for example:

Or, something like this:

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {

Solution 2:

Yes you can simply say:

function getID(oObject) 
    var id =;
    alert("This object's ID attribute is set to \"" + id + "\"."); 

Check this out:
ID Attribute | id Property

Solution 3:

This would work too:


(If element where the 1st paragraph in your document)

Solution 4:

Super Easy Way is

  $('.CheckBxMSG').each(function () {
            var ChkBxMsgId;
            ChkBxMsgId = $(this).attr('id');

Tell me if this helps

Solution 5:

In events handler you can get id as follows

function show(btn) {
  console.log('Button id:',;
<button id="myButtonId" onclick="show(this)">Click me</button>

Solution 6:

You need to check if is a string to avoid getting a child element

var getIdFromDomObj = function(domObj){
   var id =;
   return typeof id  === 'string' ? id : false;

Related:  How can I set multiple CSS styles in JavaScript?