question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to Add Data Attribute for Custom Value with Tag

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
clockwiseqcommented, Sep 11, 2017

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?

0reactions
stale[bot]commented, Mar 13, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found