Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing my imag src using ajax getting the location from db

How can I change the img src of the img using the ajax. I have the location save in my database and I want to set the img src from the value of something data[0]['patient_photo'] I have the html of like this from my image :

<img id="checkup_pic" src="">

Here is my ajax code :

success: function(data) {
        $('#valfname').text(data[0]['patient_fname']);
        $('#valmname').text(data[0]['patient_mname']);
        $('#vallname').text(data[0]['patient_lname']);
        $('#valad').text(data[0]['patient_address']);
        $('#valcont').text(data[0]['patient_contact_info']);
        $('#valsex').text(data[0]['patient_sex']);
        $('#valcvilstat').text(data[0]['patient_civil_status']);
        $('#valbday').text(data[0]['patient_bday']);
        $('#valage').text(data[0]['patient_age']);
        $('#valheight').text(data[0]['patient_height']);
        $('#valweight').text(data[0]['patient_weight']);

        $('#checkup_pic').attr("src","");
like image 860
Jc John Avatar asked Dec 07 '22 20:12

Jc John


2 Answers

You need to use attr property of jquery and pass your src in the value.

Try below code:

$('#checkup_pic').attr("src",data[0]['patient_photo']);
like image 146
Manthan Dave Avatar answered Dec 10 '22 23:12

Manthan Dave


You could use jQuery functions prop() or attr() to set property to the DOM elements :

prop() : Set one or more properties for the set of matched elements.

attr() : Set one or more attributes for the set of matched elements.

So you need just to pass the source of your image data[0]['patient_photo'] to the src attribute of your img tag identified by #checkup_pic, like :

$('#checkup_pic').prop("src", data[0]['patient_photo']);
//Or
$('#checkup_pic').attr("src", data[0]['patient_photo']);

Hope this helps.

like image 26
Zakaria Acharki Avatar answered Dec 10 '22 22:12

Zakaria Acharki