0

I would like to run a function when a div content(an image) is charged, I have been trying this with html().length and onChange, but onChange only works when page is loaded, thanks:

My code:

if (($("#zv1").html().length > 0) && ($("#zv1").change())){
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv2").html().length > 0) ) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv3").html().length > 0) && ($("#zv3").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv4").html().length > 0) && ($("#zv4").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv5").html().length > 0) && ($("#zv5").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv6").html().length > 0) && ($("#zv6").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv7").html().length > 0) && ($("#zv7").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv8").html().length > 0) && ($("#zv8").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }

AJAX example:

function sync_vitrasa(id){
    $.ajax({
     url: "/vitrasa_state/",
     type:"GET",
     data: {
       id: id,
     },success: function( data ) {
        id="#zv"+id;
        if($(id).html().length == 0){
            $(id).html(data)
            alert("sound");
        }
        else {
            if($(id).html() != data) {

                //ion.sound.play("water_droplet_2");
                alert("image is already there");
            }
        }
     }
});
}

Check if submit in another HTML is clicked:

$(document).ready(function() {
          setInterval(function () {
                $.ajax({
                    url : '/vitrasa/',
                    success : function(data){
                        $('#sub2').click(function() {
                            alert( "calling Ajax." );
                            sync_vitrasa(2);
                        });
                    }
                });
            }, 500);

            });

PD: zvX is the div id for the HTML td that contais the image charged with AJAX

Andoni Da Silva
  • 1,161
  • 15
  • 31
  • afaik a `div` doesn't have a `change` event. Especially not on programatically changes. You could trigger the event manually, but I think this is not what you are looking for ... – eisbehr Aug 17 '16 at 06:45
  • 2
    Possible duplicate of [Jquery Event : Detect changes to the html/text of a div](http://stackoverflow.com/questions/15657686/jquery-event-detect-changes-to-the-html-text-of-a-div) – Amar Singh Aug 17 '16 at 06:46

1 Answers1

1

Why track the td html changes when you know it's being changed through AJAX?

For instance, if you have something in your ajax response that says

$('#zv8').html(result);

Call whatever you need to change from there. You already know what's changing.

Edit: Change to your ajax code

I think something like this should get you what you want. This basically says if the element is empty put in the image. If it's not empty check if what I'm about to put in is different than what's in there. If it is, do something.

function sync_vitrasa(id){
    $.ajax({
     url: "/vitrasa_state/",
     type:"GET",
     data: {
       id: id,
     },success: function( data ) {
        id="#zv"+id;
        if($(id).html(length) == 0){
            $(id).html(data)
        {
        else {
            if($(id).html != data) {
                $(id).html(data);
                //ion.sound.play("water_droplet_2");
                alert("sound");
            }
        }
     }
});
Jhorra
  • 6,233
  • 21
  • 69
  • 123
  • ok, then I would like that ajax only response me with the content and my function if the content is not already loaded in the td, could you check my ajax code? thanks – Andoni Da Silva Aug 17 '16 at 07:04
  • thanks, it seems good, but when I try to load with ajax my another html where is the submit that I have to check if imput is clicked, in order to call ajax which insert the image, it does not work. I have updated my code, thanks @Jhorra – Andoni Da Silva Aug 17 '16 at 08:29
  • If I check the submit click is not necesary to check in the HTML div content – Andoni Da Silva Aug 17 '16 at 08:36