I am trying to get an href array from this page http://vm1.culture.ru/abramtsevo/catalog , but this is not how it goes:

$('.catalog__body__items .catalog__body__items__item__image').find('a[href]').each(function() { hrefs.push($(this).attr('href')); }) 

and so does not work:

 $('.catalog__body__items .catalog__body__items__item__image').find('a[href]').each(function() { hrefs.push(this.href); }) 

Only I receive an array with a tags (with attributes).

  • Try to specify in find simply (a) you then in a cycle vseravno pull out the href from them - quaresma89
  • @ quaresma89, so does an empty array come. - Timur Musharapov pm
  • Only I receive an array with a tags (with attributes). - where do you get it? how do you check? - Grundy
  • In the console on the specified page. - Timur Musharapov
  • In the console on the specified page - what exactly are you checking and how ? - Grundy

2 answers 2

Instead of passing each in this case, you can use the map method followed by a call to the get method.

 var hrefs = $('.catalog__body__items .catalog__body__items__item__image') .find('a[href]') .map(function() { return this.href; }) .get(); 

As a result, hrefs will have an array of attribute values.

With this approach there will be no ambiguity where the result is the processing, and where the result is the selection of elements.

  • Very good implementation! - Timur Musharapov

$('.catalog__body__items__item__image .catalog__body__items__item__name') and if such a selector?

 $('.catalog__body__items__item__image .catalog__body__items__item__name').each(function(){ console.log($(this).attr('href')); }); 

This is how my href shows all 9 links in the console, I don’t know what’s wrong with you there. enter image description here

Here is a screenshot showing you.

  • The same result. - Timur Musharapov
  • It can't be, I launched 9 links from me - quaresma89
  • Is it possible to complete the request, which you can enter the console to immediately check? - Timur Musharapov
  • I corrected the answer with the code. Check - quaresma89
  • pp.vk.me/c837331/v837331797/47c4/iNPw6rZkObU.jpg - and what does the array below mean? - Timur Musharapov