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
Tryd3.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
Es6 Variable Import Name in Node.Js
JavaScript Require() Function Giving Referenceerror: Require Is Not Defined
Difference Between Knockout View Models Declared as Object Literals VS Functions
How to Get the Function Name from Within That Function
Why Does JavaScript Object Show Different Values in Console in Chrome, Firefox, Safari
React This.Setstate Is Not a Function
Using Settimeout on Promise Chain
How to Listen to the Form Submit Event in JavaScript
Add Event Listener on Elements Created Dynamically
Building a Promise Chain Recursively in JavaScript - Memory Considerations
Cancel a Vanilla Ecmascript 6 Promise Chain
How to Append <Script></Script> in JavaScript
Why Does Usestate Cause the Component to Render Twice on Each Update
Why Is My Infinite Loop Blocking When It Is in an Async Function
Document.Getelementsbyclassname().Innerhtml Always Returns "Undefined"