Question

I'm not sure is this possible. Selecting multiple element is easy, but today I doubt whether this is possible or not:

$(this).closest("li, + $(this)").css('text-decoration','line-through');

The above code surely not working. It's just for the demo of the idea. I want to strike the entire li (text and the checkbox) so I selected the li together with the checkbox.

Was it helpful?

Solution

$(this).closest("li").andSelf().css('text-decoration','line-through');

or

$(this).closest("li").addBack().css('text-decoration','line-through');

for jQuery 1.8 and above

OTHER TIPS

.add()

$(this).closest("li").add(this).css('text-decoration','line-through');

You can use .add() function to add selectors:

$(this).closest("li").add(this).css('text-decoration','line-through');

The cleanest solution is to use addBack, which is dedicated to this usage :

$(this).closest("li").addBack().css('text-decoration','line-through');

I assume your $(this) represents checkbox here. If it's inside li then, you could use

$(this).parent('li').css('text-decoration','line-through');

This would have cover full li along with checkbox.

$(this).closest("li").andSelf().css('text-decoration','line-through');

Use andSelf.

Description: Add the previous set of elements on the stack to the current set.

Code:

$(this).closest("li").andSelf().css('text-decoration','line-through');

I read now in the docs that:

Note: This function has been deprecated and is now an alias for .addBack(), which should be used with jQuery 1.8 and later.

So use addBack if your are on jQuery 1.8 or later.

Code:

$(this).closest("li").addBack().css('text-decoration','line-through');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top