jQuery get index of current object

Posted: September 14, 2011 in Jquery
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
</head>
<script language="javascript">
$(function() { 
	$(".outerDiv a").click(function(){		
		var currIndex  = $(this).parent().prevAll().length;
		alert(currIndex);	
	});
});
</script>
<body>
<div class="outerDiv"><br />1 : One <a href="#">Click here</a></div>
<div class="outerDiv"><br />2 : Two <a href="#">Click here</a></div>
<div class="outerDiv"><br />3 : Three <a href="#">Click here</a></div>
<div class="outerDiv"><br />4 : Four <a href="#">Click here</a></div>
<div class="outerDiv"><br />5 : Five <a href="#">Click here</a></div>
</body>
</html>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s