Link to home
Create AccountLog in
Avatar of Brad Bansner
Brad Bansner

asked on

Use jQuery to find next element by class

Using this HTML:

<p>
	<div><a class="catmenu" href="#">Category 1</a></div>
	<div class="submenu" id="submenu1">
		<a href="0">0</a><br>
		<a href="1">1</a><br>
		<a href="2">2</a><br>
		<a href="3">3</a></div>
</p>

Open in new window


I'm trying to make it so when you click a class="cat menu" I can do something with the next class="submenu". I tried this:

<script language="javascript" type="text/javascript">
	$(function(){
		$(document).on('click', '.catmenu', function(){
			var ul=$(this).nextAll('.submenu');
			console.log('ID = '+ul.attr('id'));
		});
	})
</script>

Open in new window


There are multiple menus like this in my HTML, so I want to control all of them with the same script. My console log is just to figure out if I've selected the DIV with class="submenu" or not.

Thank you!
SOLUTION
Avatar of forethought
forethought

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Brad Bansner
Brad Bansner

ASKER

I tried changing that, but all i get is this in the console log:

ID = undefined
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Got it to work. Thanks!