-1

I am trying to create an event handler. I want to click the div tag and the a tag is also clicked, which should redirect to Google.com

This is my code:

    <div id="testTrigger"> Test Div </div>
    <a id ="TestButton" href="https://www.google.com/webhp?hl=en&sa=X&ved=0ahUKEwi5lY3N9Z3hAhUD9YMKHRjPCJcQPAgH" role="button">Test Button</a>

    <script>
    $( document ).ready(function() {
        $( "#testTrigger" ).click(function() {
          $("#TestButton").click();
        });
    });
    </script>

I tried all the ways I can think of, but nothing works.

Any help is appreciated. Thank you

Huy Nguyen
  • 21
  • 5

1 Answers1

0

You need to add [0] to the click trigger.

 $( document ).ready(function() {
   $( "#testTrigger" ).click(function() {
     $('#TestButton').get(0).click();
   });
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 
 <div id="testTrigger"> Test Div </div>
 <a id ="TestButton" href="https://www.google.com/webhp?hl=en&sa=X&ved=0ahUKEwi5lY3N9Z3hAhUD9YMKHRjPCJcQPAgH" role="button">Test Button</a>
Cory Fail
  • 1,070
  • 8
  • 26