Skip to content
Advertisement

Javascript redirect when clicking on a div with a certain id

Here is an example of what I need help with: http://www.arbutusroofing.com/roofing-services/

When the user clicks on “READ MORE” (under each subject or category) and the light grey box pops up with more information, I want it to automatically redirect to another page in 10 seconds.

I know how to redirect to a different page when an actual link is clicked, but not with certain events like this.

Here is some example of the code:

<h6 id="displayText" style="margin-top: 0px; cursor: pointer;"> 
<u>READ MORE</u> ABOUT FLAT ROOFING 
</h6>

I’m trying to redirect to a different page after 10 seconds when the id “displaytext” is clicked on.

Also, here is the code for the toggle text if you were wondering:

<script type="text/javascript"> 

    $(document).ready(function() {
      $("div#toggleText").hide();
      $("h6#displayText").click(function(){
        $(this).toggleClass("active");
        $(this).next("div#toggleText").slideToggle();
      });
    });
</script>

Advertisement

Answer

You JS code:

   function OpenNewTab(id){
    setTimeout(function(){ window.location.href = "http://yourlinkhere/'+id+'"; }, 10000);

}

HTML:

<div onclick="OpenNewTab(1)">Readmore</div> // here 1 SHOULD BE UNIQUE ID AS PER RECORDS
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement