Storing a variable via .text () is not canceled

I have tags <span>in divs that are deleted when the user clicks on them. It works great.

I want to save .text()inside this div in a variable. The problem is that the updated text is not saved.

Click on a word to remove it in jsFiddle .

As you can see, the variable contentreturns the old text, not the new changed one.

How to save a variable with updated text?

JQuery

jQuery(document).ready(function() {

    jQuery(document).on("mousedown", ".hello span", function() {

        // don't add full stop at the end of sentence if it already ends with
        var endChars = [".", "?", "!"];

        jQuery(this).fadeOut(function(){
            var parentObj = jQuery(this).parent();
            jQuery(this).remove();
            var text = parentObj.find("span").first().html();
            parentObj.find("span").first().html(ta_capitalizeFirstLetter(text));
            text = parentObj.find("span").last().html();
            if ( endChars.indexOf(text.slice(-1))  == -1 )
            {
                parentObj.find("span").last().html(text+".");
            }
        });

        var content = jQuery(this).parent().parent().find('.hello').text();

        alert(content);

    });
});
+4
source share
3 answers

fadeOut. . DOM.

// Cache the element
var $el = jQuery(this).parent().parent().find('.hello');

jQuery(this).fadeOut(function () {
    jQuery(this).remove();
    // Irrelevant code removed from here
    ...

    var content = $el.text();
    alert(content);
});

, .

+4

jsfiddle chrome, , :

  • - jQuery(this).fadeOut(function(){

  • div var content = jQuery(this).parent().parent().find('.hello').text();.

  • .

  • fadeout

, , , 2 fadeout

+1

:

jQuery(this).fadeOut(function(){
  var parentObj = jQuery(this).parent();
  jQuery(this).remove();
  var text = parentObj.find("span").first().html();
  parentObj.find("span").first().html(ta_capitalizeFirstLetter(text));
  text = parentObj.find("span").last().html();
  if ( endChars.indexOf(text.slice(-1))  == -1 ) {
    parentObj.find("span").last().html(text+".");
    var content = parentObj.parent().find('.hello').text();
    alert(content);
  }
});
+1

All Articles