1

I want to trigger uper div element using jquery

<div class="abc">
<a href="abc.com" class="test"></a>
</div>

<div class="hit_uper_div">
     abc
</div>

Onclick of hit_uper_div i want to trigger a tag inside of abc div

jQuery('.hit_uper_div').click(function(event){
    console.log("hii");
    var a = jQuery('body').find('.abc a.test').text();
    console.log(a);
    jQuery('body').find('.abc a.test').trigger('click');
  });

Anyone have idea what exact wrong in this.

Praff
  • 109
  • 4
  • 8

1 Answers1

0

Invoking the click event (which does not perform the redirect) and navigating to the href position are two different things.

Your code only invokes the click method not redirect to your given URL. To navigate the given URL see the below example. In the example, if you don't want to open the URL in a new tab you can also use window.location = $a.attr('href');.

jQuery('.hit_uper_div').click(function(event) {
  var $a = jQuery('body').find('.abc  a.test');
  window.open($a.attr('href'), '_blank');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="abc">
  <a href="https://google.com/" class="test" target="_blank"></a>
</div>

<div class="hit_uper_div">
  abc
</div>

Snippet blocks my example to redirect a new URL, so here is the working fiddle link.

4b0
  • 21,981
  • 30
  • 95
  • 142