September 8, 2013

multiple selector to include back $(this)?

Merralee Mandel’s 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.

$(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

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

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!