11

I've got a bunch of buttons nested within div tags in my html

<div class="button-wrapper">
    <button class="button-class">Click here</button>
</div>
<div class="button-wrapper">
    <button class="button-class">Click here</button>
</div>
...

Clicking on the button posts some data using JQuery and returns some HTML through a .ajax success function. I'm trying to replace the content of the div for each button that is clicked, but for some reason my JQuery selector doesn't seem to be working.

I'm trying the following within my success function:

$(this).parent().html(data);

I know that the data is being posted successfully and that the success function is working. What's weird is that the following works:

$(".button-wrapper").html(data);

but obviously this adds the HTML to every div, not just the one for the button that is clicked.

Does anyone know where I might be going wrong here?

2
  • 1
    it seems like this should work however I think you are having scope issues, we can't tell this unless you post the whole .ajax function, please post the function in its entirety Commented Jul 8, 2011 at 0:40
  • 1
    It sounds like another issue of the keyword this. Could you post the script which does the insert job and let us see? We can't say much without taking a look at it. Commented Jul 8, 2011 at 0:40

5 Answers 5

15

In jQuery 'this' is commonly overwritten within a callback. The workaround is to reference this using another variable:

var _this = this; //or var $_this = this;
$(_this).parent().html(data);
Sign up to request clarification or add additional context in comments.

Comments

4

The problem is that in your callback this is not the button anymore. Try this:

$(".button-wrapper").click(function(){
   var that=this;
   $.ajax(
      // ...
      success: function(data){
         $(that).parent().html(data);  
      }
   );
});

1 Comment

+ 1 thanks, this works great! There were a couple of similar answers so just went with the first answer that was posted.
2

This will helpfull for you....

$("button").click(function(){
    $("div").html("Hello <b>world</b>!");
}); 

1 Comment

The question is about a reference to the correct this inside a jQuery callback. This answer is irrelevant (except for your friends to serial upvote) -1
0

Html:

<div class="button-wrapper">
    <button class="button-class">Click here</button>
</div>
<div class="button-wrapper">
    <button class="button-class">Click here</button>
</div>

JS:

  $.ajax({
//..
      success: function(){
         $(".button-class").click(function(){
           $(this).parent().html("Dynamic Html");
        });
      }
    });

1 Comment

thanks @kmb385, but just fyi i tested that out and it actually didn't work.
0

You should know that you need to load log of the success function eg: setInterval(myFunction,2500);

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.