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
1
  • @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) Commented Oct 3, 2015 at 2:33

2 Answers 2

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>

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

3 Comments

this solution seems to include the "x" that is in the select2-selection__choice__remove span. which i don't want to include
check the console not the html ..
Please provide an explanation, not just code.
1

Use the .class selector:

$('.select2-selection__choice').each(function() {
    var inneText = $(this).children().text();        
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.