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.

[select2 4.0.0-beta3] Display a link to add a new item if no results are found

See original GitHub issue

In previous versions, the formatNoMatches option could be used to provide a link to add a new item if no matches were found:

formatNoMatches: function(term) {
  return "<a href='add_item?term="+term+"' class='new_item'>Add New Item</a>";
 }

However when trying to replicate this using 4.0-beta3’s new languages.noResults function:

language: {
  noResults: function() {
    return "<a href='some_URL' class='new_item'>Add New Item</a>";
 }
}

the raw unformatted string appears in the dropdown, rather than a link. Manually creating a DOM object in the function and returning that also doesn’t work, though I wouldn’t really expect it to. Is there another way to achieve this functionality in 4.0? Thanks.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
KarlKlcommented, May 5, 2015

@JayatiTehri use language->noResults and escapeMarkup options to customice the no result text. 😉

$('select').select2({
     language: {
             noResults: function() {
                 return "<a href='http://google.com'>Add</a>";
            }
     },
    escapeMarkup: function (markup) {
        return markup;
    }
});
2reactions
wpangestucommented, Jun 16, 2020

@icrm-amusienko this is how i showing term

language: { 
  noResults: function () {
    var term = event.target.value;
    return '<a href="#">add item ' + term + '</a>' 
  },
  escapeMarkup: function (markup) {
        return markup;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Select2: how to add a link instead of "No results found" text?
If you're using version 4 or newer of select2, try this: $('#cow_id_2').select2({ allowClear: true, escapeMarkup: function (markup) { return ...
Read more >
Add, select, or clear items | Select2 - The jQuery replacement ...
The fourth parameter sets the options actual selected state - if set to true , the new option will be selected by default....
Read more >
Common problems - The jQuery replacement for select boxes
Common problems. Select2 does not function properly when I use it inside a Bootstrap modal. This issue occurs because Bootstrap modals tend to...
Read more >
Get searchbox values in case of not found any value in select 2
I want to add the not found data in the list of select box options. So for that i want to get he...
Read more >
Retrieving selections | Select2 - The jQuery replacement for ...
Selected items can also be accessed via the :selected jQuery selector: ... Select2 does not add the selected attribute when an element is...
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