jQuery data attribute selector for closest parent
try this (work for #2): data-selector
e.g: $('[data-testing] , :data(testing)')
You only have two choices:
As you mentioned in the first choice, you have to iterate through all of the elements because $("#ElementID").data("testing", "data value");
will NOT update the attribute data-testing
, because the value is stored internally in the DOM.
The second method is to provide add another class that can be used in the selector:
$("#ElementID").data("testing", "data value").addClass("has-testing");
Thus your new selector would be:
$("#buttonID").closest("[data-testing], .has-testing");
JS Fiddle Example