jQuery Tutorial

jQuery Examples

jQuery Practice

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

In this tutorial, you'll understand how to get the text of the selected option tag?
Submitted by Pratishtha Saxena, on June 10, 2022

jQuery is a JavaScript library used to simplify HTML DOM tree traversal, event handling, etc.

To find the id of an element - we'll use the attr() method of jQuery. attr(), here means attribute if an element. It will return the attribute of the element provided as a parameter for it. The ID, here can be treated as an attribute of the element.



The element is specified first and the attribute name (here ID) is specified which will return the id of the element.

Note: Always put the jQuery CDN link while working with jQuery.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

jQuery example for getting the ID of an element

<!DOCTYPE html>
<html lang="en">
      <meta charset="UTF-8">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
      <div id="firstID">This is first div. It's ID is in console.</div>
         let first = $('#firstID');
         console.log(first.attr('id') );


Example: Get the ID of an element

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.