Reputation: 243
I have a problem understanding what is wrong here. I use jQuery to find an element, but I fail finding the element's parent without using jQuery:
$(".selectbox").on("click", function(e) {
e = e || window.event;
var eTarget = e.target || e.srcElement;
find_parent(eTarget);
});
using above to call find_parent function works:
function find_parent (el) {
element_parent = el.parentElement;
}
but if use this to call find_parent it fails:
function someFunction {
selectBoxEl = $(".xyz").find('.selectbox');
find_parent (selectBoxEl);
}
I am not looking for the solution of "why not using jQuery parent()" - and it works fine. I am trying to understand why normal DOM methods don't work on an element returned by jQuery and what I am doing wrong.
Edit:
Sample code above is not the actual code being used, but a very simplified version to illustrate my question. The actual find_parent()
function is a long and complicated piece that goes through many elements and their parents for various updates and it doesn't use jQuery. I want to leverage that code and not duplicating it for my need in the sample function someFunction()
.
Upvotes: 0
Views: 85
Reputation: 348
As already you are using jQuery, there is no need use separate function to identify parent DOM element. $('#id').parent()
returns parent DOM element.
Still if you want to use .parentElement javascript method, use below method.
$('#id')[0].parentElement
JQuery is not actually returning any DOM element, it will return only instance of DOM object. parentElement propery has to be apply only for DOM element not for instance object.
Upvotes: 1
Reputation: 254
The element returned by jQuery is wrapped. Use find_parent(selectBoxEl[0]);
Upvotes: 2