Brogrammer
Brogrammer

Reputation: 177

Getting the current search term from select2 input

I would like to get the current search term value (even if it is not matched) from a select2 input.

<div class="select2-container select2-container-multi form-control" id="s2id_author">
    <ul class="select2-choices">  
        <li class="select2-search-field">    
            <label for="s2id_autogen1" class="select2-offscreen"></label>    
            <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen1" placeholder="" style="width: 34px;" aria-activedescendant="select2-result-label-2">  
        </li>
    </ul>
</div>

Upvotes: 1

Views: 9247

Answers (8)

gioch
gioch

Reputation: 109

$(selector).data("select2").results.lastParams.term

Upvotes: 1

ahmed waleed
ahmed waleed

Reputation: 517

Try this worked for me

$('#select2').data("select2").$dropdown.find('.select2-search__field').val();

Upvotes: 1

Chenna
Chenna

Reputation: 2623

Works with v4.0.5

$('#select2_id').find('.select2-search__field').val();

Upvotes: 1

Daniel Campos
Daniel Campos

Reputation: 971

This one is working for me in 4.0+

var nome = $("#select2_id").data('select2').$dropdown.find("input").val();

Upvotes: 9

nigal
nigal

Reputation: 181

You can try this to get the last search term:

$(selector).data('select2').lastSearchTerm

Upvotes: -1

Siva.G ツ
Siva.G ツ

Reputation: 839

Check the below, it may help your need

var InputSelector = '#e1',
select2 = $(InputSelector).data('select2'),
searchedInput = select2.search;

Upvotes: 1

Abhishekh Gupta
Abhishekh Gupta

Reputation: 6236

You can try this:

$(selector)
  .data("select2")
  .search[0]
  .value;

Upvotes: 5

Brian H
Brian H

Reputation: 256

you can try this

$('input.select2-input').val();

this will get you the first value in a set of values

My question back to you is, are there more than one 'Select2-Input' fields on your form?

Upvotes: 0

Related Questions