Cody
Cody

Reputation: 8944

Can't set HTML using jQuery

For some reason, my script isn't writing out the text after I remove the textbox element. Am I incorrectly using the .html or is something else wrong?

        $('.time').click(function () {
            var valueOnClick = $(this).html();
            $(this).empty();
            $(this).append("<input type='text' class='input timebox' />");
            $('.timebox').val(valueOnClick);
            $('.timebox').focus();
            $('.timebox').blur(function () {
                var newValue = $(this).val();
                var dataToPost = { timeValue: newValue };
                $(this).remove('.timebox');
                if (valueOnClick != newValue) {
                    $.ajax({
                        type: "POST",
                        url: "Test",
                        data: dataToPost,
                        success: function (msg) {
                            alert(msg);
                            $(this).html("88");
                        }
                    });
                } else {
                    // there is no need to send 
                    // an ajax call if the number
                    // did not change
                    alert("else");
                    $(this).html("88");
                }
            });
        });

OK, thanks to the comments, I figured out I was referencing the wrong thing. The solution for me was to change the blur function as follows:

            $('.timebox').blur(function () {
                var newValue = $(this).val();
                var dataToPost = { timeValue: newValue };
                if (valueOnClick != newValue) {
                    $.ajax({
                        type: "POST",
                        url: "Test",
                        data: dataToPost,
                        success: function (msg) {
                        }
                    });
                } else {
                    // there is no need to send 
                    // an ajax call if the number
                    // did not change
                }
                $(this).parent().html("8");
                $(this).remove('.timebox');
            });

Upvotes: 0

Views: 219

Answers (3)

SoWhat
SoWhat

Reputation: 5622

The value of this changes if you enter a function. So when u use this in the blur function handler, it actually points to '.timebox'

    $('.time').click(function () {
        var valueOnClick = $(this).html();
        var $time=$(this);//If you want to access .time inside the function for blur
                          //Use $time instead of$(this)

        $(this).empty();
        $(this).append("<input type='text' class='input timebox' />");
        $('.timebox').val(valueOnClick);
        $('.timebox').focus();
        $('.timebox').blur(function () {
            var newValue = $(this).val();
            var dataToPost = { timeValue: newValue };
            $(this).remove(); //Since $(this) now refers to .timebox
            if (valueOnClick != newValue) {
                $.ajax({
                    type: "POST",
                    url: "Test",
                    data: dataToPost,
                    success: function (msg) {
                        alert(msg);
                        $(this).html("88");
                    }
                });
            } else {
                // there is no need to send 
                // an ajax call if the number
                // did not change
                alert("else");
                $(this).html("88");
            }
        });
    });

Upvotes: 0

halit
halit

Reputation: 1128

$(this) = '.timebox'  element but you have removed it already, 

  $.ajax({
                        type: "POST",
                        url: "Test",
                        data: dataToPost,
                        success: function (msg) {
                            alert(msg);
  $(this).html("88");  // This = msg
                        }

and 

 else {
                    // there is no need to send 
                    // an ajax call if the number
                    // did not change
                    alert("else");
                    $(this).html("88"); // this = '.timebox'  element but you have removed it already, 
                }

Upvotes: 0

Johan
Johan

Reputation: 35194

$(this) in your success handler is refering to msg, not $('.timebox') (or whatever element that you want to append the html to)

Upvotes: 3

Related Questions