0

The image and caption, in class story has two different links. Image points to one location and link to another.

I need to make whole class story into a link.

<div class="story">
<a href="link"> <img src="linkforimage"></a> 
<a href="link2">Caption for image </a>
</div>

I need a solution like this:

<a href="link">
  <div class="story">
    <a href="link"> <img src="linkforimage"></a> 
    <a href="link2">Caption for image </a>
  </div>
</a>
roscioli
  • 1,200
  • 3
  • 15
  • 33
abzalassembekov
  • 81
  • 1
  • 4
  • 11

3 Answers3

2

Nesting a tags is a bad idea. I would use a delegate here. Have a wrapper a tag and in JS check which source span was clicked.

html

<a href="#" class="story">
    <span data-href="link"><img src="linkforimage"/></span> 
    <span data-href="link2">Caption for image</span>
</a>

js (jquery based)

$('.story').on('click', 'span', function() {
  document.location.href= $(this).data('href');
});
Koen Peters
  • 12,798
  • 6
  • 36
  • 59
0

You can't have anchor tag inside anchor tag. You can use onclick method to achieve your goal.

<div onClick="window.open(link);" class="story">
<a> <img src="linkforimage"></a> 
<a onClick="window.open(link2); event.stopPropagation();" >Caption for image </a>
</div>

EDIT jsfiddle

Anoop
  • 23,044
  • 10
  • 62
  • 76
0

Clarifying the problem I am facing right now is that I am using embedly platform, where image comes from . So, I just post with link and image are parsed from the link by embedly. But, that ahref takes user to original web site. Caption takes user to the page with that content on my web site. I need to make of caption to the whole div.

So, I found the answer with CSS, I just added: style=" pointer-events: none;cursor: default;" on the link with image and style="cursor: pointer;" onclick="window.location='link'" on a class "story" in div.

abzalassembekov
  • 81
  • 1
  • 4
  • 11