I am trying to build up a jQuery set using add() but my implementation is not adding elements to the set.
var questionPath = $(); // create empty set
var selectedRuleElement = $(event.currentTarget);
questionPath.add(selectedRuleElement); //no element is added to set here!
selectedRuleElement.find('li.property-value-node').each(function () {
var questionId = $(this).attr('data-parent-id');
questionPath.add($('#' + questionId)); // or here!
});
return questionPath;
Both of the above add() lines are given a valid element as an argument. Can someone tell me the correct way to add elements to a jQuery set?
Like strings, jQuery objects are immutable, so you need to re-assign the returned value. Otherwise you’re discarding the value returned by
.add()andquestionPathremains unaltered.