Xhevat Ziberi
Xhevat Ziberi

Reputation: 19

select the link from the title with jquery in a post

var src = jQuery('.ovh header h2 a').attr("href");
jQuery('.entry-content').after('<a href="' + src_should_be_here + '">Continue reading >></a>');

I am using this code to add links to all the posts but I cant get the href from the link in the title which is like this:

header > h2 > a 

and the entry-content class is right after the header div. I tried this:

var src = jQuery('.ovh header h2 a').attr("href");

but it selects only the link of the first post not every post

HTML:

<header>
  <div class="entry-meta post-info">
    <!-- <span class="byline author vcard">By <a href="http://appfessional.com/new/author/admin1/" rel="author" class="fn">Kitejock Team</a>,</span>-->
    <span class="post-tags"><a href="http://appfessional.com/new/tag/canada/" rel="tag">canada</a>&nbsp;<a href="http://appfessional.com/new/tag/gear-2/" rel="tag">gear</a>&nbsp;<a href="http://appfessional.com/new/tag/np/" rel="tag">np</a></span>
    <a href="http://appfessional.com/new/satisfy-your-thirst-with-np-2014-collection/#respond" title="Comment on Satisfy Your Thirst with NP 2014 Collection">Leave a comment</a>
  </div>

  <h2><a href="http://appfessional.com/new/satisfy-your-thirst-with-np-2014-collection/">Satisfy Your Thirst with NP 2014 Collection</a></h2>
</header>

<div class="entry-content">
  <p>NP brings &nbsp;the 2014 collection of waterwear and accessories, all designed to enhance the riding experience and to provide comfort, protection and style to every rider. Share their 40 year obsession for water and decide for yourself. www.npsurf.com</p>
</div>

Upvotes: 1

Views: 93

Answers (2)

Dejv
Dejv

Reputation: 954

If I understand this correctly, you have html that looks like this:

<div class="ovh">
  <div class="header">
     <h2>
      <a href="some-link">Lorem Ipsum</a>
     </h2>
  </div>
  <div class="entry-content"></div>
</div>

And you would like to find each entry-content and after it you would like to add link that you get from each .ovh header h2 a

In that case you can do:

$('.entry-content').each(function(){
  var href = $(this).prev().find('h2 a').attr("href");
  $(this).after('<a href="' + href + '">Continue reading >></a>');
});

By this you simply navigate through DOM to find the proper element.

Upvotes: 0

adeneo
adeneo

Reputation: 318302

Iterate over the .entry-content elements, then find the related href and insert each new anchor after the current .entry-content

jQuery('.entry-content').each(function() {
    var href   = $(this).prev('header').find('h2 > a').attr('href'),
        anchor = $('<a />', {href: href, text: 'Continue reading >>'});

    $(this).after(anchor);
});

Upvotes: 1

Related Questions