3

I have div2(height 20px vertical align centre) inside div1(100px) and div2 having anchor tag.

On hover of div1 I am showing hand pointer, but also want on click anywhere in div1 area it should navigate to anchor tag url. Please refer Image for more detail

.test:hover {
  cursor: pointer;
  cursor: hand;
}
<div class="test" style="height:100px">
  <div style=" position: relative; top: 50%; transform: translateY(-50%);">
    <a href="google.com" ; target="_blank" title="This link opens in a new window" style="color:#2e3940;text-decoration: initial;">Pawan Kotak</a>
  </div>
</div>

enter image description here

5
  • 4
    Please post your actual HTML and CSS, along with the code you wrote to attempt to solve this yourself, within the question Commented Nov 29, 2016 at 10:10
  • <!DOCTYPE html> <html> <head> <title>Page Title</title> <style> .test:hover{ cursor: pointer; cursor: hand; } </style> </head> <body> <div class="test" style="height:100px"><div style=" position: relative; top: 50%; transform: translateY(-50%);" ><a href="google.com" target="_blank" title="This link opens in a new window" style="color:#2e3940;text-decoration: initial;" > Pawan Kotak</a></div></div> </body> </html> Commented Nov 29, 2016 at 10:23
  • @RoryMcCrossan I have attached the code Commented Nov 29, 2016 at 10:24
  • Thanks. In future, please use the edit button in your question so the code can be formatted properly. I've done this for you in this case Commented Nov 29, 2016 at 10:24
  • @RoryMcCrossan thanks a lot. Commented Nov 29, 2016 at 14:07

4 Answers 4

4

If the outer 'div' should behave like an anchor, just reorder the elements like this:

<a href="myAnchor.tld">
  <div id="div1">
    <div id="div2"></div>
  </div>
</a>

You don't have to mess around with jQuery, if your markup could simply be structured the way it actualy works.

Sign up to request clarification or add additional context in comments.

Comments

2

try with following way

$(div1).on("click",function(){
   window.location = $(this).find("a").attr("href"); 
});

this is just a wayout.

Comments

2

Assuming your div id's to be div1 and div2 here is a solution.

$('#div1').on('click',function(){
  $(this).find('a').trigger('click');
});

The script will attach a click event to the div1 and when ever user clicks this div it finds the a tag with in it $(this).find('a') and triggers a click event on that anchor tag.

2 Comments

multiple times event get fired one for div and one for anchor tag when i click on anchor tag..how can i avoid multiple trigger of even
@pawankotak you can do event.stopimmediatepropagation() which will stop the bubbling of the events..
1

you can also grow your anchor tag (there are several methods/approaches)

.test {
  background-color: red;
}
.test a {
  cursor: hand;
  background-color: blue;
  display: block;
  line-height: 100px;
}
<div class="test" style="height:100px">
  <div style=" position: relative; top: 50%; transform: translateY(-50%);">
    <a href="google.com" ; target="_blank" title="This link opens in a new window" style="color:#2e3940;text-decoration: initial;">Pawan Kotak</a>
  </div>
</div>

1 Comment

I tried your solution but in my case some multi line text was also present in anchor tag which was creating problem. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.