Mark Klein  Fermin Mark Klein Fermin - 4 months ago 10
Javascript Question

Update multiple picture src from their different figcaption

Here is the HTML I'm trying to put the src of the img files dynamically when loaded.

<figure>
<img src="" class="img-rounded" width="220" height="221">
<figcaption>Ubing</figcaption>
</figure>


<figure>
<img src="" class="img-rounded" width="220" height="221">
<figcaption>Lalaki</figcaption>
</figure>
<figure>
<img src="" class="img-rounded" width="220" height="221">
<figcaption>Babai</figcaption>
</figure>


Here is the Jquery. Did I correctly selected the img here? Does the jquery update all of this when it works?

$(document).load(function() {
var ex = $('figcaption').text();
$.post('../php/fetchLabel.php' , {input:ex} , function(data){
if(data== false){
$('this').prev().attr('alt','No Data In database');
}
else{
$('this').prev().attr('src', '../'+data);
}
})})


Someone please help me!

Answer
$(document).load(function() {
    $("figure").each(function(){
     var $figure = $(this);
     var ex = $figure.find('figcaption').text();
     var $image = $figure.find("img");
    $.post('../php/fetchLabel.php' , {input:ex} , function(data){
        if(data== false){
            $image.attr('alt','No Data In database');
        }
        else{
            $image.attr('src', '../'+data);
        }
})
})
})