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 );

e.g.
.attr("title");
.attr("id");

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">
<html>
	<head>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
	<script type="text/javascript"> 
		$(document).ready(function () {
			$('.test').click(function() { 
		 		alert('' + $(this).attr('id'));
			});
		}); 	
	</script> 
	</head>
	<body>
		<input type="button" value="Button A" class="test" id="buttonA"> 
		<input type="button" value="Button B" class="test" id="buttonB">
	</body>
</html>

Tags Jquery 


HTML Comment Box is loading comments...

Hi I am Yashwant Chavan founder of www.technicalkeeda.com, Purpose of this website to share the programming knowledge in the form post , blogs and articles.

Latest Updates

Stay updated with latest post, articles and technical discussions.

Most Popular Articles



© technicalkeeda.com 2014