1

I have this html code and i want to get the values of the select2-search-choice div values

<div class="select2-container select2-container-multi form-control multi-select" id="s2id_project_banker_ids">
  <ul class="select2-choices">  
     <li class="select2-search-choice">    
       <div>Sam Chong</div>    
       <a href="#" class="select2-search-choice-close" tabindex="-1"></a>  
     </li>
     <li class="select2-search-choice">    
       <div>Beh Li Shiew</div>    
       <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
     </li>
     <li class="select2-search-field">    
       <label for="s2id_autogen23" class="select2-offscreen"></label>    
       <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen23" name="s2id_autogen24" placeholder="" data-validate="true" aria-activedescendant="select2-result-label-198" style="width: 42px;">  
     </li>
   </ul>
</div>

So far what i did was this which seems to work but i am looking for a more efficient way of executing this code

project_bankers = []
$.each($('#s2id_project_banker_ids').find('li.select2-search-choice'), function(index, choice) {
  project_bankers.push(choice.children[0].innerHTML)
})

So just checking is there any more efficient way to do this?

1
  • You got a lot of answers what are working, but you ask for efficiency. So I create a benchmark in my second answer. @KingsleySimon Commented Jul 20, 2016 at 12:17

7 Answers 7

3

You can use map() to generate an array from a set of elements in a jQuery object:

var project_bankers = $('#s2id_project_banker_ids li.select2-search-choice div').map(function() {
  return $(this).text();
}).get();

console.log(project_bankers);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select2-container select2-container-multi form-control multi-select" id="s2id_project_banker_ids">
  <ul class="select2-choices">
    <li class="select2-search-choice">
      <div>Sam Chong</div>
      <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
    </li>
    <li class="select2-search-choice">
      <div>Beh Li Shiew</div>
      <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
    </li>
    <li class="select2-search-field">
      <label for="s2id_autogen23" class="select2-offscreen"></label>
      <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen23" name="s2id_autogen24" placeholder="" data-validate="true" aria-activedescendant="select2-result-label-198" style="width: 42px;">
    </li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

map() is the most elegant solution. It's more gentlemanly than just pushing stuff in an array.
0

You can do it like this:

project_bankers = []
$.each($('#s2id_project_banker_ids li.select2-search-choice'), function(index, choice) {
  project_bankers.push(choice.children[0].innerHTML)
})

And it's even better if you can remove the li from your selector $('#s2id_project_banker_ids .select2-search-choice') this is more efficient.

Comments

0

You can target the divs more tightly:

project_bankers = [];

$('.select2-search-choice > div').each(function() {
  project_bankers.push($(this).text())
})

but I would really recommend tightenig up the code at the list end rather than at the jQuery end. - for example - putting the class on the element you wantto target (the div inside the li).

Comments

0

use this script it works

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
    $(function(){
        var array = [];
        var i = 0;
        $('.select2-search-choice div').each(function(){
            array[i] = this.innerHTML;
            i++;
        });
        alert(array);
    });
</script>

3 Comments

You could just use push() (like the other three answers already) instead of setting the index explicitly
I wrote a very simple code that is easily understanding, so i used this assignment operator.
That makes it even more complex instead of simple. ;)
0

Asking for the most efficient way, you have to compare $.each, $.map and the native solution provied by javascript.

If you really want to be as fast as possible, you should use plain javascript, and no jQuery at all. It might be not mutch, but the question was to be most efficient. Then there is only one way, pure js.

var project_bankers = [];
var elements = document.getElementsByClassName("select2-search-choice");

for( var i = 0, l = elements.length; i < l; i++ )
    project_bankers.push(elements[i].children[0].innerHTML);

View live Benachmark.

Comments

0

Don't use find and a child select, just select the elements directly with a direct selector:

var project_bankers = [];
$('#s2id_project_banker_ids .select2-search-choice div').each(function() {
    project_bankers.push($(this).text());
});

Or completly without jQuery, plain javascript. It is the fastes way too.

var project_bankers = [];
var elements = document.getElementsByClassName("select2-search-choice");

for( var i = 0, l = elements.length; i < l; i++ )
    project_bankers.push(elements[i].children[0].innerHTML);

1 Comment

haha - thats basically what I did in my post - one small typo (from the OP - not you- missing the semicolon after the array declaration. Small point, though and nice to see that someone else does it the way I do it :)
-2

Yes, you don't need the each because it is an ID and you can only have one ID on the page so each doesn't make sense.

var val = $('li.select2-search-choice').text(), project_bankers = [];
project_bankers.push(val);

2 Comments

@eisbehr how come?
Because 1.) You didn't understand the question 2.) ID makes it faster 3.) Your code only works for one single entry, but therer are multiple 4.) If it WOULD be like you did, why use push instead of [val] ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.