I would like to select all elements those have
- an ancestor with class X, and
- 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
I would like to select all elements those have
By some reason. I need to use 'on' method of jquery So please tell me an string which I can use with 'on' method
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