Question with jQuery method chaining .closest().toggleClass

Question with jQuery method chaining .closest().toggleClass
0.0 0

#1

I want to be able to only toggle a class for the closest span element. What is the correct syntax? I’m a bit fuzzy still on the use of this in event-listener functions.


#2

First, you need to put some elements in your index.html file. You are referencing non-existent elements at this point. Explain what you think you are trying to do with the jQuery code you have written so far.


#3

I was just using repl.it to show the snippet of JS.

This is the full project I’m working on:

Basically, I want to be able to only toggle a class on the parent span element of the ‘check’ button that is being toggled.


#4
$(this).closest('li').toggleClass('shopping-item__checked');

#5

Thanks for the help. It got me going in the right direction. The solution you gave was dashing-through both the list item title and the check button, but I found the solution to only check through the list item:


#6

Yes, I noticed the dash after I posted it. Glad you figured it out.