0

Basically what i have done is, i have made a web advert in after effects and i am looking to put it in HTML, If the user clicks on my ad, they get taken to a companion website of your choice. So i want the entire div that its in to be clickable and to make it link to another site. using javascript.



    <!-- Meta -->
    <meta charset="UTF-8" />
    <title>Bodymovin Demo</title>

    <!-- Styles-->
    <style>
        #bm{max-width: 970px:margin: 0 auto;}
     </style>

</head>
<body>

    <div id="bm"></div>





    <!-- Scripts -->
    <script src="lottie.js"></script>
    <script src="anim.js"></script>

The expected result is once you click on the web advert, you are directed to another site.

Liam
  • 27,717
  • 28
  • 128
  • 190
00 11
  • 49
  • 5
  • you can add the `onclick=""` method on the div and trigger an event (like linking to another site) For your situation should be `onclick="window.location.href="http://yourwebsite.com"` – Facyo Kouch Oct 21 '19 at 09:53
  • [When we have exhausted our own knowledge, it’s often tempting to simply ask someone else to solve the problem for us. It is very common to find the question we have is one many people have already experienced, and many of these people have already asked about it and have received correct answers in response. Because of the vast amount of information on the internet and Stack Overflow, it often takes just a simple search or two to find them.](http://idownvotedbecau.se/noresearch/) – Liam Oct 21 '19 at 09:54
  • https://stackoverflow.com/questions/20772954/add-click-event-on-div-tag-using-javascript – Amanjot Kaur Oct 21 '19 at 10:05

3 Answers3

2

You can use the following Javascript:

<script>
    const adv_div = document.querySelector('#bm');
    const adv_url = 'https://www.google.com';
    adv_div.addEventListener(
        'click',
        () => {
            window.open(adv_url);
        }
    );
</script>

To change the cursor to a hand(pointer) when you hover on the element use this CSS:

#bm {
    cursor: pointer;
    max-width: 970px;
    margin: 0 auto;
}
Elben
  • 173
  • 1
  • 8
0

Put it inside anchor tag

<a href="link"> 
    <div id="bm"></div> 
</a>
Himanshu Poddar
  • 7,112
  • 10
  • 47
  • 93
0

Just create an onclick function on the div. Whenever there is a click inside the div the function will be called

function a()
{
console.log("Div clicked")
}
#bm {
  max-width: 970px:margin: 0 auto;
}
<div id="bm" onclick="a()">dfsfsdfsd sdfsdf sdfsd fsd fs dfsd
</div>
ellipsis
  • 12,049
  • 2
  • 17
  • 33