How to access img of a JQUERY figure?

Asked

Viewed 110 times

-1

I want to get the $(figure img).attr('src'); current, but the editor is saying that this is wrong and will not work. how to make it work?

HTML CODE:

<figure class="effect-oscar  wowload fadeInUp" fotografo='Jessiane'>
        <img src="http://iphotochannel.com.br/wp-content/uploads/2015/09/15_1.jpg" class='caraDoFotografo' width="450" height="297" alt="img01"/>
        <figcaption>
            <h2>Jessiane</h2>

            <p>FotoGrafia É Vida <br>
            2 FOTOS<BR>

        <a href=''>Ver Fotos</a>          
        </figcaption>

    </figure>

I have this JS so he’s wrong there in $(this img):

$('figure').on('click',function() {
    ft = $(this).attr('fotografo');
link = $(this img).attr('src');
window.location.href='blend/portfolio.php?fotografo='+ft+'&link='+link;

});

3 answers

1

The mistake is that you have to understand what it is string and what is not string, without the quotation marks:

$(figure img).attr('src');

It’s like waiting for a variable called figure, the correct one should be with the quotation marks:

$('figure img').attr('src');

This is also wrong, because you are wanting to mix the this which is a native javascript object for scoping access with the word img:

$(this img).attr('src');

This is probably what’s right:

$('figure').on('click',function() {
    var ft = $(this).attr('fotografo');
    var link = $('img', this).attr('src');
    window.location.href='blend/portfolio.php?fotografo='+ft+'&link='+link;
});

The this is passed after the comma to change the "context" of the selectors in the function $(...) jQuery, meaning instead of using the selector 'img' in the document. he will use in the <figure> that you clicked, as explained in the documentation:

  • Correct, the semantic use would be this way var link = $('img', this).attr('src');, if I may, I will re-edit my answer by adding this way.

0

To select the atributo src, do as follows:

link = $('figure img').attr('src');

You can’t access it the way you’re doing it link = $(this img).attr('src'); for img is not enclosed in quotes, so the browser can not find the reference, and other, can not mix the this with an element html this way you’re doing.

To use the this in the appropriate way in that situation var link = $('img', this).attr('src'); as Guilherme quoted it.

-1

Try performing by swapping for the class of the photo:

$('.caraDoFotografo').attr('src');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.