Scott
Scott

Reputation: 4200

Get id attribute from another element in same list item with Jquery

<li class="ui-li ui-li-static ui-body-c">
    <p class="ui-li-aside ui-li-desc"></p>
    <span id="122"></span>
    <h3 class="ui-li-heading"></h3>
    <p class="ui-li-desc"></p>
    <p class="ui-li-desc"><a class="ui-link">Report</a></p>
</li>

So here is my HTML markup.

Goal: I need JQuery code that will give me the id value of the <span> element (so in this case: 122) within the same <li>, when I click the <a> in that same <li>.

How would I do this?

Thanks!

Upvotes: 3

Views: 465

Answers (2)

Alex R.
Alex R.

Reputation: 4754

If you have only one span as in your html...

$('a.ui-link').live('click', function(){
   var id = $('span', $(this).closest('li')).attr('id');
});

Upvotes: 1

Phil.Wheeler
Phil.Wheeler

Reputation: 16848

Assuming you only have one span tag in your list items, you should be able to get your span's id with this:

$('a.ui-link').click(function(){
   var id = $(this).closest('li').find('span:first').attr('id');
});

Upvotes: 2

Related Questions