How to access img of a JQUERY figure?

-1

I want to get the current $(figure img).attr('src'); , 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 it'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;

});
    
asked by anonymous 26.02.2018 / 14:01

3 answers

1

The error is that you have to understand what is string and what is not string , if it goes like this without the quotation marks:

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

As if expecting a variable called figure , the correct one should be with the quotation marks:

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

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

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

The right thing would probably be this:

$('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 of jQuery, ie instead of using the 'img' selector in document. it will use in <figure> you clicked, as explained in the documentation:

26.02.2018 / 14:32
0

To select atributo src , do the following:

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

You can not access the way you are doing link = $(this img).attr('src'); because img is not enclosed in quotation marks, so the browser can not find the reference, and another, you can not mix this with a html element % this way you're doing it.

To use this as appropriate in this situation, the right would be var link = $('img', this).attr('src'); as Guilherme quoted.

    
26.02.2018 / 14:32
-1

Try changing photo class:

$('.caraDoFotografo').attr('src');
    
26.02.2018 / 14:20