I have simple code where is 4 containers with buttons. Now I tried to make whole element clickable to redirect to site specific to the container.
One element like I have in example working fine but when I add more than one, others will not work.
There is facebook, youtube, google, and twitter container and each of them have specific link to redirect.
So I need to make more than one element correctly clickable not only on a button but on whole element.
var el = document.getElementById("container");
if (el.addEventListener) {
el.addEventListener("click", function() {
document.getElementById("button").click();
}, false);
} else {
el.attachEvent("onclick", function() {
document.getElementById("button").click();
});
}
#container {
background: coral;
padding: 32px;
margin: 16px;
cursor: pointer;
}
<div id="container">
<a id="button" href="https://google.sk/">Google</a>
</div>
<div id="container">
<a id="button" href="https://youtube.com/">Youtube</a>
</div>
<div id="container">
<a id="button" href="https://facebook.com/">Facebook</a>
</div>
<div id="container">
<a id="button" href="https://twitter.com/">Twitter</a>
</div>
<a href="google.sk"><div class="container">google</div></a>doing what you're currently doing in your code won't work