Lord Tubington Lord Tubington - 10 days ago 4
HTML Question

Create array of parent element's text based on child element's string match

Trying to search a massive member list (1200+) and find which of my admins added each member. I want the console to return an array of the names of all the members that were added by X, where X is the admin's name. That way I can compile a spreadsheet to keep track of which members were added by corresponding admin.

Example HTML I'm working with.

<div class="_6a _5u5j _6b">
<div class="fsl fwb fcb"><a href="">Jessica Smith</a></div>
<div class="_5aj7">
<div class="_4bl9">
<div class="_17tq">Model at Model/Actress</div>
<div class="fsm fwn fcg">Joined
<br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 4 months ago</span></abbr></div>
</div>
<div class="_4bl7 mrm"></div>
</div>
</div>

<div class="_6a _5u5j _6b">
<div class="fsl fwb fcb"><a href="">Robyn Stevens</a></div>
<div class="_5aj7">
<div class="_4bl9">
<div class="_17tq">No Job</div>
<div class="fsm fwn fcg">Joined
<br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 2 months ago</span></abbr></div>
</div>
<div class="_4bl7 mrm"></div>
</div>
</div>

<div class="_6a _5u5j _6b">
<div class="fsl fwb fcb"><a href="">Sarah West</a></div>
<div class="_5aj7">
<div class="_4bl9">
<div class="_17tq">Entertainer</div>
<div class="fsm fwn fcg">Joined
<br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 3 months ago</span></abbr></div>
</div>
<div class="_4bl7 mrm"></div>
</div>
</div>


And here's the jQuery I'm trying to execute in the console

$("div.fsm:contains('Jane Doe')").find("div.fsl a").each(function() {
console.log($(this).text());
})


But I'm not making any headway here. Here's a fiddle I'm playing around in https://jsfiddle.net/w9vfq62r/3/ any help to get this fiddle example working would be greatly appreciated.

Answer

Here you go, which will parse all the admins nicely and append the corresponding members to them into the admin array.

Updated with timestamp as well.

var admins = {};
var refText = 'Added by';
$('div.fsm').each(function() {
    var self = $(this);
    var addedByText = self.text().replace('\n', ' ').replace(/\s{2,}/, ' ');
    var startIndex = addedByText.indexOf(refText) + refText.length;
    var admin = $.trim(addedByText.substring(startIndex, addedByText.indexOf('about')));
    var member = self.closest('._6a').find('div.fsl a').text();
  
    if(!admins[admin]) {
      admins[admin] = [];
    }
  
    admins[admin].push(member);
    admins[admin].push(self.find('abbr').attr('title'));
});

console.log(admins);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Jessica Smith</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">Model at Model/Actress</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 4 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>

<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Robyn Stevens</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">No Job</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 2 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>

<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Sarah West</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">Entertainer</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 3 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>