Link to home
Start Free TrialLog in
Avatar of Brad Bansner
Brad Bansner

asked on

Use jQuery/Javascript to select parent DIV by class

I am trying to get the ID of the parent <div> when "return to top" is clicked, using the following code:

console.log($(this).parent('p').prevAll('.subpage').attr('id'));

Here is my markup:

<div id="mvi2" class="subpage">
	<p>Lots of content here.</p>

	<p><a class="subpagetop" href="javascript:void(0)">RETURN TO TOP</a></p>
</div>

Open in new window


It just keeps returning "undefined", though. What am I doing wrong?

Thanks!
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

You already are at the correct element with .parent so...

console.log($(this).parent('div').attr('id'));

...should do it.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep. Caught my mistake, but too late to correct it. I should have said...

console.log($(this).parent('p').parent('div').attr('id'));
Avatar of Brad Bansner
Brad Bansner

ASKER

Thanks so much!
'welcome! have a nice week-end!