I have a context menu where i want to use certain method it find p
/div
,so right now i'm just logging so that to see if i find proper element when i click.
html
<div>Hello I'm div</div>
<p>I'm para <span>I'm span inside para</span></p>
code
var paraElm = $target.closest("p")
if(paraElm && paraElm[0]){
console.log("i have selected the element is para",paraElm)
}
it give me an array in output
log
for para
i have selected the element is para,
n.fn.init [p.locked.sender, prevObject: n.fn.init(1),context: p.locked.sender]
0: p.locked.sender
context: p.locked.sender
length: 1
prevObject:n.fn.init [p.locked.sender, context: p.locked.sender]
[[Prototype]]: Object(0)
log
for span inside para
i have selected the element is para,
n.fn.init [p.locked.sender, prevObject: n.fn.init(1), context: span.selected]
0: p.locked.sender
context: span.selected
length: 1
prevObject: n.fn.init [span.selected, context: span.selected]
[[Prototype]]: Object(0)
So when i click on span i don't want it log or get that element just on p tag only
CodePudding user response:
// i hope this solve your issue
$(document).ready(function() {
$("body").on('click', function(event) {
editTask(event.target);
});
});
function editTask(node) {
var closest_element = $(node).text();
console.log(closest_element);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Hello I'm div</div>
<p>I'm para <span>I'm span inside para</span></p>
CodePudding user response:
UPDATE: So i made some changes with the help of condition i used worked for :D just in JSFiddle it isn't showing properly, it works fine for me.
$(document).ready(function() {
$("body").on('click', function(event) {
var $target = $(event.target);
var getElm = $target.closest('p');
if (getElm.context.nodeName.toLowerCase() != 'span') {
console.log("I got the element", getElm);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Hello I'm div</div>
<p>I'm para <span>I'm span inside para</span></p>