Brad Bansner
asked on
Use jQuery to find next element by class
Using this HTML:
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:
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!
<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>
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>
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Got it to work. Thanks!
ASKER
ID = undefined