As Brian points out, you could access the dataset property to retrieve all the element's data-* attributes:
$('.activityItem').each(function () { var dataAttributes = this.dataset; Object.keys(dataAttributes).forEach(function (key) { console.log(key, dataAttributes[key]); // key, value }); });
Since you only want the first data attribute, you could simply use:
var dataAttributes = this.dataset; var id = dataAttributes[Object.keys(dataAttributes)[0]];
It's important to note that the dataset property returns the attribute names in camel case without the data prefix. In other words, data-question-id would be questionId.
If you want to retrieve all of an element's attributes, access the attributes property and check which attributes start with data-* and end with -id. It's definitely more verbose, but it may work better in other scenarios.
$('.activityItem').each(function () { var attributes = this.attributes; Object.keys(attributes).forEach(function (key) { var attribute = attributes[key]; if (/^data-.*-id$/.test(attribute.name)) { console.log(attribute.name, attribute.value); } }) });
data()method for those too?-id?.activityItemelement has exclusively one of these but I won't know which one before-hand.