I’m looking to combine some code but I’m not sure if what I want to do is possible. Basically I have a function with a bunch of if/else statements, and there is one line that is similar throughout all the different cases:
$('#selector).prev('.class-selector').addClass('current');
$('#selector).next('.class-selector').addClass('current');
$('#selector).last('.class-selector').addClass('current');
$('#selector).first('.class-selector').addClass('current');
Are there any ways I can set the filter method (prev, next, last, first) as a variable so I don’t have to repeat this line all over my function? I tried passing a parameter to the function and using a variable as the method:
$('#selector).variable('.class-selector').addClass('current');
but that doesn’t work. I get:
object has no method ‘variable'” error.
I vaguely understand why this doesn’t work, based on how jQuery code is read. Is there another way to do this?
This is not much shorter, but it’s definitely more condensed 🙂