Consider the following HTML:
<div class="group">
<div class="select">Project Type</div>
<div class="option">
<div>Personal Website</div>
<div>Business Website</div>
<div>Hosting</div>
<div>Social Media</div>
<div>Online Storefront</div>
<div>Graphics & Branding</div>
</div>
</div>
Here is my working jQuery:
var item = $('.option').find('div');
item.on('click', function(event){
var test = $(this).parent().prev('.select');
console.log( test );
});
This works, but my problem is that I don't think this is very flexible, as it can easily break if I re-arrange the code in any way, as .select must be adjacent and previous to the .option or it breaks.
Does anyone have any good solutions that could help me traverse the DOM tree a little more dynamically, so as not to depend on the exact order of the HTML elements?