Milen Mihalev
Milen Mihalev

Reputation: 350

div onclick inside anchor tag

I have a code like this:

<a href="somewhere.html">
<img src="blabla">
<div class="wrap">
    <div id="id1" onclick="window.location = 'somepage1.html';"></div>
    <div id="id2" onclick="window.location = 'somepage2.html';"></div>
<div>
</a>

The problem is that the div's onclick is not working. When I click on div's I go to somewhere.html.

Upvotes: 4

Views: 5313

Answers (2)

Sarfraz
Sarfraz

Reputation: 382881

You have wrong html structure wrapping divs inside link (unless using html5):

<a href="somewhere.html">
<img src="blabla">
<div class="wrap">
    <div id="id1" onclick="window.location = 'somepage1.html';"></div>
    <div id="id2" onclick="window.location = 'somepage2.html';"</div>
<div>
</a>

When you click, you will always go to link at top somewhere.html and inner divs won't do what you want to do. You should modify your html structure.

Upvotes: 3

Headshota
Headshota

Reputation: 21449

<a href="somewhere.html" onclick = "return false;">
<img src="blabla">
<div class="wrap">
  <div id="id1" onclick="window.location = 'somepage1.html';"></div>
  <div id="id2" onclick="window.location = 'somepage2.html';"</div>
<div>
</a>

Upvotes: 0

Related Questions