1

How come this works:

jQuery(this).closest("li").find("p").text();

But when I have it inside a function, it doesn't work:

jQuery.each(words, function(i, v) {
    jQuery(this).closest("li").find("p").text();
});

Do I need to pass this through the function?

Full Code:

jQuery(document).ready(function() {
    jQuery('.js-trigger-editb').bind("mouseup", function() {

        // find the p tag that contains the content and split it
        var words = jQuery(this).closest("li").find("p").text().split(" ");

        // wrap words in p tag into span tags
        jQuery.each(words, function(i, v) {
            jQuery(this).closest("li").find("p").append(jQuery("<span>").text(v));
        });

    });
});
laaposto
  • 11,835
  • 15
  • 54
  • 71
Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155
  • @Pete Please post an answer outlining this approach. Not sure if I understand how you mean to *cache*... – Henrik Petterson Apr 26 '16 at 10:55
  • @HenrikPetterson - Please see the answer I posted for a way to pass along `this`. – techfoobar Apr 26 '16 at 10:59
  • @HenrikPetterson something like this: https://jsfiddle.net/kwav6Le0/ – Pete Apr 26 '16 at 11:04