There may be a simple answer to this question, but for the life of me, I don’t understand why the following two methods of using $.add() give me a different result set.
var toBeSorted;
if (contextEl.hasClass("lv"))
toBeSorted = $(".lv", contextEl).add(contextEl);
else
toBeSorted = $(".lv", contextEl);
versus
var toBeSorted = $(".lv", contextEl);
if (contextEl.hasClass("lv"))
toBeSorted.add(contextEl);
When the IF statement is true, I always get one more element in the top code segment than in the bottom code segment (namely contextEl is in the result set – which is exactly what I want). I don’t understand why the bottom method’s way of calling toBeSorted.add(contextEl) doesn’t do the trick?
Any pointers or advice is much appreciated.
Thank you!
You need to write the resulting object after
.add()back totoBeSortedas you do in the first code block.