I am trying to obtain the id of the container whose href element was clicked, using jQuery. I know this involves use of the parent() method, but I’m not sure how to put it all together.
Here is a snippet of the kind of HTML doc I will be processing:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div id="cntnr_1" class="container">
<a href="/foo1">foo1</a>
<a href="/foo2">foo2</a>
<a href="/foo3">foo3</a>
</div>
<div id="cntnr_2" class="container">
<a href="/foobar1">foobar1</a>
<a href="/foobar2">foobar2</a>
<a href="/foobar3">foobar3</a>
</div>
<script type="text/javascript">
$().ready(function(){
//display the url of the clicked on link
//display the id of the container whose href was clicked on
});
</script>
</body>
</html>
The desired behavior (When a link [href] in a “container” is clicked) is as follows:
- Display the link of the url clicked on (so I can submit an AJAX POST to the url)
- Display the id of the container (so I can push the received data back to that id)
Can anyone help with how to obtain the parent container id and the url that was clicked?
[[Edit]]
I have posted the actual HTML code I am using here: http://jsfiddle.net/fpPRL/
UPDATE:
Since you tried it on a different html markup (and didn’t work), here’s a more universal solution (see it in action here : http://jsfiddle.net/fpPRL/1/):