1

I have 50 divs like the below, and the image is not clickable. I would like to make the image clickable, depending on what link the div contains: it is always the href link in the article_title tag.

How can I achieve this with adding just a few lines of JavaScript?

I was thinking of adding an ID to each img tag manually, and then use getElementById and do some magic, but that would take too long.

<div class="col-md-4 article">
      <img src="7tips.jpg" alt="Canary Wharf" title="7 Tips For Lasting In Your Career" width="324" height="235"> 
      <div class="article_category">
         <a href=”http://www.canarywharfian.co.uk/forums/wealth-management/”>Wealth Management</a> 
      </div>
      <div class="article_meta">
         <h1 class="article_title"><a href="http://www.canarywharfian.co.uk/threads/7-tips-for-lasting-in-your-career.242/" rel="bookmark" title="7 Tips For Lasting In Your Career">7 Tips For Lasting In Your Career</a></h1>
         <div class="col-md-6 author_date"><a href="http://www.canarywharfian.co.uk/members/smallcappm.1140/">SmallCapPM</a> - <span class="date"> Mar 10, 2016</span></div>
         <div class="article_abstract">
            Many of you are hoping and planning for a long and successful career in finance...
         </div>
      </div>
   </div>
D. Richard
  • 460
  • 3
  • 12

3 Answers3

2

Maybe this (using jQuery):

<script>
    $(document).ready(function () {
        $('.block').each(function( index, value ) {
            let href = $(this).find('a[href]').attr('href');
            $(this).find('img').wrap('<a href=' + href + '></a>');
        });
    });
</script>
user218046
  • 623
  • 6
  • 20
0

if you use jQuery, you can simply wrapping the images with element.

$(function(){
  // traversing all the images
  $('img').each(function(){
    // get the link by finding from parent
    var link = $(this).parent().find('.article_title a').attr('href');
    // replace the image with link + image
    $(this).replaceWith('<a href="' + link + '">' + $(this)[0].outerHTML + '</a>');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class="col-md-4 article">
      <img src="http://www.canarywharfian.co.uk/7tips.jpg" alt="Canary Wharf" title="7 Tips For Lasting In Your Career" width="324" height="235"> 
      <div class="article_category">
         <a href="http://www.canarywharfian.co.uk/forums/wealth-management/">Wealth Management</a> 
      </div>
      <div class="article_meta">
         <h1 class="article_title"><a href="http://www.canarywharfian.co.uk/threads/7-tips-for-lasting-in-your-career.242/" rel="bookmark" title="7 Tips For Lasting In Your Career">7 Tips For Lasting In Your Career</a></h1>
         <div class="col-md-6 author_date"><a href="http://www.canarywharfian.co.uk/members/smallcappm.1140/">SmallCapPM</a> - <span class="date"> Mar 10, 2016</span></div>
         <div class="article_abstract">
            Many of you are hoping and planning for a long and successful career in finance...
         </div>
      </div>
   </div>
Fal
  • 135
  • 6
  • This is very good, but it returns the href in the article_category div. I need the one in the article_title. – D. Richard Feb 17 '17 at 08:39
  • You are a god. Thanks so much. – D. Richard Feb 17 '17 at 08:43
  • I have another question. Can you tell me how can I remove the "canarywharfian.co.uk" part of the href in the article_category div dynamically? I need to remove that part otherwise if a user clicks canarywharfian.co.uk/canarywharfian.co.uk/path will load which is not ok. – D. Richard Feb 17 '17 at 08:50
  • @D.Richard with this function, you can get the domain >> http://stackoverflow.com/a/23945027/7572603. then you just simply use that to edit href >> $('[href]').each(function(){ link = $(this).attr('href'); domain = extractDomain(link); new_href = link.replace(domain, ''); $(this).attr('href', new_href); }) – Fal Feb 17 '17 at 08:58
  • This one loops through ALL the hrefs? I only need it for the first appearing one within the div. – D. Richard Feb 17 '17 at 12:26
0

Per your tag of vanila Javascript, what you can do is to search for and cache all your articles. Within each article search for anchor inside your title and set its href to a newly created anchor. Append the image into the newly created anchor and prepend it into your article.

A simple example (H1 font-size reduced for demo):

var articles = document.getElementsByClassName('article');

[].forEach.call(articles, function(article) {
 var image = article.querySelector('img:first-child');
 var title = article.querySelector('h1.article_title > a');
 var anchor = document.createElement('a');
 anchor.href = title.href;
 anchor.appendChild(image);
 article.insertBefore(anchor, article.firstChild);
});
h1 { font-size: 12px; }
<div class="col-md-4 article">
      <img src="//placehold.it/120x64" alt="Canary Wharf" title="7 Tips For Lasting In Your Career"> 
      <div class="article_category">
         <a href="example.com">Wealth Management</a> 
      </div>
      <div class="article_meta">
         <h1 class="article_title"><a href="//bing.com" rel="bookmark" title="7 Tips For Lasting In Your Career">7 Tips For Lasting In Your Career</a></h1>
         <div class="col-md-6 author_date"><a href="example.com">SmallCapPM</a> - <span class="date"> Mar 10, 2016</span></div>
         <div class="article_abstract">
            Many of you are hoping and planning for a long and successful career in finance...
         </div>
      </div>
</div>
<hr/>
<div class="col-md-4 article">
      <img src="//placehold.it/120x64" alt="Canary Wharf" title="7 Tips For Lasting In Your Career"> 
      <div class="article_category">
         <a href="example.com">Wealth Management</a> 
      </div>
      <div class="article_meta">
         <h1 class="article_title"><a href="//google.com" rel="bookmark" title="7 Tips For Lasting In Your Career">7 Tips For Lasting In Your Career</a></h1>
         <div class="col-md-6 author_date"><a href="example.com">SmallCapPM</a> - <span class="date"> Mar 10, 2016</span></div>
         <div class="article_abstract">
            Many of you are hoping and planning for a long and successful career in finance...
         </div>
      </div>
</div>

Notice, how the links to bing and google are picked up from the title anchors and inserted into a new anchor wrapping the images.

Abhitalks
  • 27,721
  • 5
  • 58
  • 81