Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    62
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Get the ID of the parent 'div'

    Hey all,

    I'm tyring to get the ID of the parent 'div' when the user clicks a link, using the following structure, but I can't seem to get it. Could someone point me in the right direction please?

    Code:
    <div id="individual-services">
    	<h2 class="js-enabled expanded_tab">
    		<ul class="ajax-links visible">
    			<li id="post-6088">
    				<a href="http://test.dynedrewett.com/services/commercial-property/">Commercial Property</a>
    			</li>
    			<li id="post-6085">
    				<a href="http://test.dynedrewett.com/services/contentious-wills-trusts-and-probate-2/">Contentious Wills, Trusts and Probate</a>
    			</li>
    			<li id="post-415">
    				<a href="http://test.dynedrewett.com/services/dispute-resolution/">Dispute Resolution</a>
    			</li>
    		</ul>
    	</h2>
    </div>
    <div id="businesses-services">
    	{same structure as above}
    </div>
    <div id="agriculture-services">
    	{same structure as above}
    </div>
    So if someone clicks 'Dispute Resolution' for example, I need to get 'individual-services'.

    I've tried both of these, but both return 'undefined' -

    Code:
    alert($(this).parent('div').prop('id'));
    alert($(this).closest('div').prop('id'));
    Thanks.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Try this

    Code:
    alert($(this).parents('div').eq(0).attr('id'));

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    62
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks, but still returning 'undefined'.

    Thanks.

  • #4
    New Coder
    Join Date
    Jun 2009
    Posts
    62
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Sorry, I had a typo when I tried your code. That is working, thanks for the help.

    Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •