user1661677
user1661677

Reputation: 1272

Getting each anchor value with Jquery

I'm trying to build a list of emails from a webpage that is formatted like so:

<div class="somediv">
  <div class="email-popdown">
    <a href="mailto:[email protected]">[email protected]</a>
  </div>
</div>

<div class="somediv">
  <div class="email-popdown">
    <a href="mailto:[email protected]">[email protected]</a>
  </div>
</div>

<div class="somediv">
  <div class="email-popdown">
    <a href="mailto:[email protected]">[email protected]</a>
  </div>
</div>

Here is my javascript that isn't working:

$('.email-popdown').each(function(i, obj) {
    $('body').append($(this).child('a').attr('href') + '<br>');
});

Any help getting this to list emails as:

[email protected]
<br>
[email protected]
<br>
[email protected]

Thanks!

Upvotes: 1

Views: 34

Answers (2)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

Use find() instead of child()

$('.email-popdown').each(function(i, obj) {
    $('body').append($(this).find('a').text() + '<br>');
});

Upvotes: 1

void
void

Reputation: 36703

Use .find() instead of .child, and .replace("mailto:", "") to strip of mailto: from href.

$('.email-popdown').each(function(i, obj) {
    $('body').append($(this).find('a').attr('href').replace("mailto:", "") + '<br>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="somediv">
  <div class="email-popdown">
    <a href="mailto:[email protected]">[email protected]</a>
  </div>
</div>

<div class="somediv">
  <div class="email-popdown">
    <a href="mailto:[email protected]">[email protected]</a>
  </div>
</div>

<div class="somediv">
  <div class="email-popdown">
    <a href="mailto:[email protected]">[email protected]</a>
  </div>
</div>

Upvotes: 1

Related Questions