G
G
GamiDPC2018-02-02 19:57:44
JavaScript
GamiDPC, 2018-02-02 19:57:44

Get data from div to log?

How to get all values ​​of href class"list-item__link" and content of class"list-item__date" in log
Should be something like this

http://element.ru/user/1 - 01.01.2018
http://element.ru/user/2 - 02.10.2017
http://element.ru/user/3 - 11.11.2017

<div class="list">
<li class="list-item ">
        <a href="http://element.ru/user/1" class="list-item__link">
        <span class="list-item__date">01.01.2018</span>
        </a>
</li>
<li class="list-item ">
        <a href="http://element.ru/user/2" class="list-item__link">
        <span class="list-item__date">02.10.2017</span>
        </a>
</li>
<li class="list-item ">
        <a href="http://element.ru/user/3" class="list-item__link">
        <span class="list-item__date">11.11.2017</span>
        </a>
</li>
</div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Grishaev, 2018-02-02
@Gigatrop

Not tested, but should work:

var links = document.querySelectorAll('.list-item__link'),
  linksArray = Array.prototype.slice.call(links),
  logArray = [];
linksArray.forEach(function(link){
  logArray.push( link.getAttribute('href') + ' - ' + link.querySelector('.list-item__date').innerHTML );
});
var log = logArray.join('\n');

G
GamiDPC, 2018-02-04
@GamiDPC

It doesn't work, this is the error

Uncaught TypeError: Cannot read property 'innerHTML' of null
    at <anonymous>:5:92
    at Array.forEach (<anonymous>)
    at <anonymous>:4:12

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question