3

I have the following html and I am trying to figure out the right selector logic to read in certain part. NOTE: I can't change the html as this is being generated by a plugin.

  <li class="select2-selection__choice" title="">
      <span class="select2-selection__choice__remove" role="presentation">×</span>
    MS Office
 </li>
 <li class="select2-selection__choice" title="">
      <span class="select2-selection__choice__remove" role="presentation">×</span>
    Photoshop
 </li>

and I am trying to read out the text inside the

 <li class="select2-selection__choice">

but NOT including the

 <span class="select2-selection__choice__remove">

so for the example above, I am looking to parse out the following text:

 MS Office, Photoshop
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • @WhiteHat - I don't have a choice as this html is being generated by a plugin so this is what i am given (thus the question) – leora Oct 03 '15 at 02:33

2 Answers2

2

Try like this

$('.select2-selection__choice').each(function() {
    var text = $(this).clone().children().remove().end().text();
    console.log(text.trim());
});

SNIPPET

$('.select2-selection__choice').each(function() {
    var text = $(this).clone().children().remove().end().text();
    console.log(text.trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="select2-selection__choice" title="">
      <span class="select2-selection__choice__remove" role="presentation">×</span>
    MS Office
 </li>
 <li class="select2-selection__choice" title="">
      <span class="select2-selection__choice__remove" role="presentation">×</span>
    Photoshop
 </li>
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
1

Use the .class selector:

$('.select2-selection__choice').each(function() {
    var inneText = $(this).children().text();        
});
Gabriel Rodriguez
  • 1,163
  • 10
  • 23