Get an Element's Id

How can I get an element's ID value with JavaScript?

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

myDOMElement.id

Or, something like this:

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
alert(inputs[i].id);
}

How can I get the ID of an element using jQuery?

The jQuery way:

$('#test').attr('id')

In your example:

$(document).ready(function() {  console.log($('#test').attr('id'));});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="test"></div>

Get element id name on html element

Try
d3.select('table').attr("id")

https://github.com/d3/d3-selection/blob/v2.0.0/README.md#selection_attr

How to get an element's ID from event.target

You can use e.target.id. e.target represents DOM object and you can access all its property and methods.

$('body').on('click', function(e){
alert(e.target.id);
});

You can convert the DOM object to jQuery object using jQuery function jQuery(e.target) or $(e.target) to call the jQuery functions on it.

Get element id by data attribute

Use each() method to iterate over elements and generate object inside based on the data attribute.

var obj = {};
// select elements with the attribute and iterate$('[data-post]').each(function() { // get data attribute value var d = $(this).data('post'); // define the property if not already defined obj[d] = obj[d] || []; // push the id of element obj[d].push(this.id)})
console.log(obj)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ul id="commentlist">  <li id="comment-12" data-post="post-1">Comment 1 for post 1</li>  <li id="comment-34" data-post="post-1">Comment 2 for post 1</li>  <li id="comment-56" data-post="post-1">Comment 3 for post 1</li></ul><ul id="commentlist">  <li id="comment-78" data-post="post-2">Comment 1 for post 2</li>  <li id="comment-90" data-post="post-2">Comment 2 for post 2</li></ul>

Getting an element's `id` attribute

The way I see it, you most probably need to attach a selector class to the ul so that you can use it to get the id:

<ul id="todo" class="todoclassname" />

then, you can get the id by doing:

$(".todoclassname").attr("id");

Create a new property and get element id?

class CustomDiv extends HTMLDivElement {
myData = {
id: '',
name: 'Caesar',
title: 'Imperator'
};
constructor() {
super();
}

connectedCallback() {
this.myData.id = this.getAttribute('id');
}
}

customElements.define('custom-div',CustomDiv , {extends: 'div'});

alert(document.getElementById("demo1").myData.id); // alert demo1

alert(document.getElementById("demo2").myData.name); // alert Cesar

alert(document.getElementById("demo3").myData.title); // alert Imperator
<div is="custom-div" id="demo1"></div>
<div is="custom-div" id="demo2"></div>
<div is="custom-div" id="demo3"></div>

Get ID of dom-element

var videoElement = document.getElementById('video');
var idOfElement = videoElement.getAttribute('id');


Related Topics



Leave a reply



Submit