Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key and value in tag? #123

Open
Goldjan opened this issue Jun 29, 2018 · 1 comment
Open

Key and value in tag? #123

Goldjan opened this issue Jun 29, 2018 · 1 comment

Comments

@Goldjan
Copy link

Goldjan commented Jun 29, 2018

Is it possible to save a key and value here?

Possible data structure:

var countries = [
  ['Germany', 'de'],
  ['USA', 'us'],
  ['Spain', 'es']
];

I want this:

<li class="taggle ">
  <span class="taggle_text">Germany</span>
  <button class="close" type="button">×</button>
  <input type="hidden" value="de" name="taggles[]">
</li>
@okcoker
Copy link
Owner

okcoker commented Jun 29, 2018

You can do something like this:

var countries = [
  ['Germany', 'de'],
  ['USA', 'us'],
  ['Spain', 'es']
];

var map = countries.reduce(function(acc, pair) {
    var code = pair[1];
    var country = pair[0];
    
    acc[code] = country;
    
    return acc;
}, {});

var example = new Taggle('example1', {
    tags: Object.keys(map),
    tagFormatter: function(li) {
    	var text = li.querySelector('.taggle_text');
        var displayText = map[text.textContent];
        
        text.textContent = displayText;
    	
        return li;
    }
});

Demo here.

I do like the idea of being able to edit the input value a bit easier though so I'll leave this issue open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants