How can I replace an image (inside a div) via jquery and ajax

I am trying to click an image and change it to another image through jquery.

When I go to the code below, the controller action is triggered on the server server and on the client side, I can see the correct html return in the firebug viewer, but the image does not change at all. any idea why this div is not updating?

original div:

<div class="inlineDiv" toggle="off" id="22"><img src="../../images/vote-favorite-off1.png" border="0"></div>

jquery code:

    $(document).ready(function() {
        $('div.inlineDiv').live('click', function() {

        var id = $(this).attr("id");
        var toggle = $(this).attr("toggle");

        var url = '/Tracker/Update?id=' + id + '&toggle=' + toggle;

        $.get(url, function(data) {
            $(this).html(data);
        });
    });
}); 

controller action:

  public ActionResult Update(int id, string toggle)
  {
       if (toggle == "off")
        {
            return Content("<img src='../../images/vote-favorite-on1.png' border=0'>");
        }
        return Content("<img src='../../images/vote-favorite-off1.png' border=0'>");
  }
+3
source share
2 answers

I believe that thisget refers to the get function.

easier:

$(this).load(URL);
+4
source

$(this) . $('div.inlineDiv').html(data)

var objDiv=$(this);
    $.get(url, function(data) {
        objDiv.html(data);
    });

$('div.inlineDiv img').attr('src',returnedPath); , src.

$('div.inlineDiv').load(URL);

0

Source: https://habr.com/ru/post/1741954/


All Articles