Unable to Add Data Attribute for Custom Value with Tag
See original GitHub issueWhen I manually add an option to the select, I need to include a custom data attribute. How is that done? When I use AJAX to get the items, this works fine:
processResults: function (data) {
return {
results: $.map(data, function (obj) {
return {
id: obj.Id,
text: obj.LastName + ', ' + obj.FirstName,
company: obj.Company
};
})
};
}
But I need to load these results back into the select2 as tags upon load and I can’t get the extra data property (company) added:
$.each(dtlPatients, function (index, patient) {
$('#PatientListControl').select2().append('<option value="' + patient.Id
+ '" data-company="' + patient.Company + '">' + patient.Name + '</option>');
});
$('#PatientListControl').val(selpatlist).trigger('change');
Am I adding the data attribute properly? Is there a better way to manually add the tags to my select2?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:16 (6 by maintainers)
Top Results From Across the Web
jquery - Can't update data-attribute value - Stack Overflow
Data is loaded into the dom and manipulated there using .data(). The attribute is not updated and should not be used to store...
Read more >Using data attributes - Learn web development | MDN
HTML is designed with extensibility in mind for data that should be ... Reading the values of these attributes out in JavaScript is...
Read more >Send custom user attributes to Intercom | Help Center
Intercom lets you create and track custom data attributes about your users and leads, based on criteria that's specific to your business.
Read more >Create custom attributes for user profiles - Google Support
Under Custom fields, create a custom attribute: Name—Enter the label you want to display on the user's account page. Info type—Select Text, Whole...
Read more >Custom attributes | Webflow University
Add a new custom attribute for the selected element. Name (data input field). Type the name of the attribute. Value ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
4877 doesn’t help at all. The links provided in the thread are just redirects to the new documentation (Configuration page). I’ll see if I can manually set the data attribute after creating the option and see if it’s available afterwards. My question is, is that the correct way to storing that custom bit of data? When I create a tag via the ajax call, how is that stored on the option? Is it a data-* attribute?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.