Reputation: 321
I do believe this is a very stupid question and there's a very simple answer for it, but I didn't found a solution:
I have the following JavaScript function:
function setStatus(order, operation, status, elem){
elem.parent().$(".active").removeClass("active");
elem.addClass("active");
}
Elem is Image-element in my HTML which I'm sure is the right one by checking it's type with alert(elem);
The error I get is exactly what I expected:
Object #< HTMLImageElement> has no method 'parent'
I already know this is because elem is a DOM-element and not an JQuery-element which means that it's not possible to use functions like parent()
and addClass()
. How can I use all JQuery-function on my elem? Is there a way to convert a DOM-element to a JQuery-element? I found this question on the internet many times with this
that should have been $(this)
, but $(elem)
doesn't seem to work.
Upvotes: 1
Views: 189
Reputation: 388316
Try
function setStatus(order, operation, status, elem){
elem.parent().find(".active").removeClass("active");
elem.addClass("active");
}
Upvotes: 2
Reputation: 74738
You have some syntax errors in your script and you can use this .addback()
to chain it.
elem.parent(".active").removeClass("active").addBack().addClass("active");
Upvotes: 0
Reputation: 15934
.parent()
is a jquery function so in order to use it, you must "jqueryify" the element you are working with, so:
$(elem)
also, the spurious $('.active')
would cause issues so the solution should be:
$(elem).parent().removeClass("active");
However, without seeing some HTML to go along with the JS, I can't be 100% sure.
Upvotes: 1
Reputation: 2528
try this
$(elem).parent().removeClass("active");
$(elem).addClass("active");
remove $(".active")
. i think this is also culprit.
Upvotes: 2