Jquery statement that returns the last child of different elements

1

Hello, I'm doing some exercises in a programming course I did. I came across this task and I do not know how to solve it, does anyone have any tips?

Make a jQuery statement that selects all the elements that are the last child in the HTML of type img OR that are the last children in HTML of type h3

    
asked by anonymous 26.01.2017 / 03:34

1 answer

2

To select more than one element in jquery, just pass other filters in the selector using the comma, for example to select the last image and the last H3 in the page I can call as follows:

var lastBandImageAndName = $('img:last, h3:last');

If I want to get all the last elements inside a div, using the same example above:

var lastBandImageAndName = $('div img:last, div h3:last');


JavaScript

$(document).ready(function()
{
    var lastBandImageAndName = $('img:last, h3:last');

    lastBandImageAndName.each(function()
    {
       var tag = $(this).prop('tagName');
       if(tag === 'IMG')
       {
          $('#result').append('Última imagem URL = '+ $(this).prop('src'));
       }
       else if(tag === 'H3')
       { 
          $('#result').append('Último H3 texto = ' +  $(this).text());  
       }    
    });  
});

I've created a Fiddle with the above code, if you'd like to have a look > > link

    
26.01.2017 / 05:06