5

I use to update Label values inside the AJAX success function like below, But I need to know how I'm going to apply this method to change/update "src" of an <img id="myimage" src=""/>

$.ajax({
    url: 'clmcontrol_livematchupdate',
    type: 'post',
    dataType: 'json',

    success: function (data) {

        $('#mstatus').html(data.matchstatus);
        // $('#myimage').... ?

    },
    complete: function () {
        // Schedule the next request when the current one has been completed
        setTimeout(ajaxInterval, 4000);
    }
});
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Dilukshan Mahendra
  • 3,230
  • 7
  • 41
  • 62

3 Answers3

10

Using jquery, You can use like $("#myimage").attr('src','img url');

Assume, you have response like data.imgsrc then it should be like, $("#myimage").attr(src, data.imgsrc);

$.ajax({
        url: 'clmcontrol_livematchupdate',
        type: 'post',
        dataType: 'json',

        success: function (data) {

            $('#mstatus').html(data.matchstatus);
            $("#myimage").attr('src','img url');

        },
        complete: function () {
            // Schedule the next request when the current one has been completed
            setTimeout(ajaxInterval, 4000);
        }
    });
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • I agree with $("#myimage").attr(src, data.imgsrc); but the src part should come inside quotes like 'src' right? cause it didn't work until I do so. – Dilukshan Mahendra Nov 21 '13 at 06:19
4
$('#myimage').attr('src', '/imagePath/');
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
swt
  • 417
  • 4
  • 3
1

Try .prop()

success: function (data) {
    $('#mstatus').html(data.matchstatus);
    $('#myimage').prop('src', 'VAlue'); //change image src
}


Read .prop() vs .attr()
Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107