3

I'm trying to select one of the parent elements through the first four letters of its id for the following two reasons:

a - The IDs vary. b - The nesting level varies.

See example:

<div id="zone1">
    <div class="child"></div>
</div>

<div id="zone2">
    <div id="something-else">
       <div class="child"></div>
    </div>
</div>

and the jQuery I want to achieve needs to find the parent div who's id starts with the letters 'zone,' as such:

$('.child').click(function(){
   $(this).parents('#zone....').doSomething();
});

Is there a way to do this? Or at least a thought on how to go about reaching that parent div?

Thanks a lot for your help.

Mo Alsaedi
  • 709
  • 3
  • 15
  • possible duplicate of [jquery selector for id starts with specific text](http://stackoverflow.com/questions/23223526/jquery-selector-for-id-starts-with-specific-text) – Jon Doe Apr 15 '15 at 20:23

1 Answers1

10

You can use the attribute starts with selector

$('.child').click(function(){
   $(this).closest('[id^="zone"]').doSomething();
});
adeneo
  • 312,895
  • 29
  • 395
  • 388