How to Get Element id Using Jquery

 Posted On  | Yashwant Chavan 

Welcome to another Jquery tutorial series, Today I am going to write about, how to get element Id using Jquery.This is small piece of code but very useful, even though I am working in Jquery from last couple of years, but still some times I forgot how to get the element Id.

Retrive Jquery Element ID

Some times there is requirement to get the id of clicked element or when check-box option is selected or dynamic elements processing. Know more about jquery .attr(), The .attr() method gets the attribute value for only the first element in the matched set, .attr() method returns undefined for attributes that have not been set. It means if you are trying to get ID attribute and id attribute of an element is not define, at that time you will get the "undefined" value.

Jquery .attr syntax

.attr( attributeName );


Jquery .attr() id Example

In below example there are two button on each button click we are going to retrieve respected button id say buttonA and buttonB.

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
 <script type="text/javascript" src=""></script>
 <script type="text/javascript"> 
  $(document).ready(function () {
   $('.test').click(function() { 
     alert('' + $(this).attr('id'));
  <input type="button" value="Button A" class="test" id="buttonA"> 
  <input type="button" value="Button B" class="test" id="buttonB">

© 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0327 seconds.