-1

I would like to get the text() values of every span. Best case scenario would involve a solution that makes use of the "event"-parameter.

$(document).ready(function () {
    $('li').click(function (e) {
      console.log($("span:nth-of-type(1n)").text());
      console.log($("span:nth-of-type(2n)").text());
      console.log($("span:nth-of-type(3n)").text());
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <ul>
    <li>
      <span>li1 + span1</span>
      <span>li1 + span2</span>
      <span>li1 + span3</span>
    </li>
    <li>
      <span>li2 + span1</span>
      <span>li2 + span2</span>
      <span>li3 + span3</span>
    </li>
  </ul>
</div>

If you have any idea, I would highly appreciate a working snippet.

The expected output would be something like:

let var1 = $("span:nth-of-type(1n)").text(); // li1 + span1
let var2 = $("span:nth-of-type(2n)").text(); // li1 + span2
let var3 = $("span:nth-of-type(3n)").text(); // li1 + span3

In the forEach-loop solution, I would need the span's text element in every loop, which I don't have.

AppRoyale
  • 403
  • 1
  • 5
  • 21
  • Your code seems to be working. Do you have an issue with it? What output are you expecting to have? – Rory McCrossan Mar 27 '17 at 14:55
  • @RoryMcCrossan: Not really, note the first `console.log` output. – T.J. Crowder Mar 27 '17 at 14:56
  • That's because it's getting the text of every `1n` span. I get that it may not be what the OP wants, but they haven't specified what they *do* want. I didn't downvote you FYI – Rory McCrossan Mar 27 '17 at 14:57
  • @RoryMcCrossan I would assume they have more than three span tags or a variable number. I think they just want to display the text from each span separately – George Mar 27 '17 at 14:57
  • It is not really a duplicate per se, since I am not looking for a solution involving a forEach loop to print all the results inside the console, but rather assign each span's text() value to a local variable. Could anybody assist please? – AppRoyale Mar 28 '17 at 14:08

2 Answers2

3

Best case scenario would involve a solution that makes use of the "event"-parameter

You can do that, but there's no need to; this within the click callback will be the li element on which the event was hooked. (But see the second snippet below if you really want to use the event parameter.)

So we can loop through the spans within the li, either using $(this).find("span") (all descendants) or $(this).children("span") (just direct children):

$(document).ready(function () {
    $('li').click(function (e) {
      $(this).children('span').each(function() {
        console.log($(this).text());
      });
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <ul>
    <li>
      <span>li1 + span1</span>
      <span>li1 + span2</span>
      <span>li1 + span3</span>
    </li>
    <li>
      <span>li2 + span1</span>
      <span>li2 + span2</span>
      <span>li3 + span3</span>
    </li>
  </ul>
</div>

If using the event parameter is really important, we can use e.currentTarget to get the same thing this gives us in the above:

$(document).ready(function () {
    $('li').click(function (e) {
      $(e.currentTarget).children('span').each(function() {
        console.log($(this).text());
      });
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <ul>
    <li>
      <span>li1 + span1</span>
      <span>li1 + span2</span>
      <span>li1 + span3</span>
    </li>
    <li>
      <span>li2 + span1</span>
      <span>li2 + span2</span>
      <span>li3 + span3</span>
    </li>
  </ul>
</div>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

https://jsfiddle.net/g998f2hv/1/

$(document).ready(function () {
    $('li').click(function (e) {
      var spans = $(this).find("span");
      console.log($(spans[0]).text());
      console.log($(spans[1]).text());
      console.log($(spans[2]).text());
    });
});
enno.void
  • 6,242
  • 4
  • 25
  • 42