Update Image source from AJAX success function

19,652

Solution 1

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);
        }
    });

Solution 2

$('#myimage').attr('src', '/imagePath/');

Solution 3

Try .prop()

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


Read .prop() vs .attr()
Share:
19,652

Related videos on Youtube

Dilukshan Mahendra
Author by

Dilukshan Mahendra

I'm a Software Engineering Graduate at Open University of Sri Lanka and have worked with several programming tools and technologies like, Java Swing, Java Web, Struts 2, Jasper Report, Netbeans 7.0, IntelliJ Idea, iReport C#, ASP.net, Crystal Reports, Visual Studio 2010 PHP 5.x, CodeIgniter, Wordpress MySQL, SQL Server Prolog, JADE, Multi-Agent Systems HTML5, CSS3, Javascript, Dreamweaver, Photoshop Testing & QA, JIRA, YouTrack

Updated on October 08, 2022

Comments

  • Dilukshan Mahendra
    Dilukshan Mahendra over 1 year

    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);
        }
    });
    
    • Aditya Singh
      Aditya Singh over 10 years
      $("#elementId").attr("src","value");
  • Dilukshan Mahendra
    Dilukshan Mahendra over 10 years
    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.