-4

I would like to select all elements those have

  1. an ancestor with class X, and
  2. an ancestor with id Y.

By some reason. I need to use 'on' method of jquery So please tell me an string which I can use with 'on' method

j08691
  • 204,283
  • 31
  • 260
  • 272
kotaro
  • 11
  • 1
  • 2
  • can you please update your question with an example code. – Ahtisham Jan 29 '19 at 15:18
  • 2
    What have you tried? Where's your code? – j08691 Jan 29 '19 at 15:18
  • Possible duplicate of [Check if any ancestor has a class using jQuery](https://stackoverflow.com/questions/17084839/check-if-any-ancestor-has-a-class-using-jquery) –  Jan 29 '19 at 15:23
  • Questions seeking debugging help should include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it *within the question itself*. Questions without a clear problem statement are not useful to other readers. Please see: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Rory McCrossan Jan 29 '19 at 15:44

1 Answers1

0

jsfiddle example

your html

<div class="container">

  <button class="check">
     check
  </button>

  <div id="y">
    <div class="x">
      test1
    </div>
  </div>

  <div id="p">
    <div class="x">
      test2
    </div>
  </div>

  <div id="y">
    <div class="p">
      test3
    </div>
  </div>

  <div id="y">
    <div class="x">
      test4
    </div>
  </div>

</div>

your JavaScript function

$(document).ready(function() {
  $(".check").click(function() {
    $('[id=y]').each(function() {
      if ($(this).find(".x").length > 0) {
        alert($(this).text());
      }
    });
  });
});

Result
test1
test4

Digvijay
  • 7,836
  • 3
  • 32
  • 53