0
<div class="news">
 <div class="news-item"></div>
 <div class="news-item"></div>
 <div class="news-item"></div>
</div>

<div class="news-info">
 <div class="info-item"></div>
 <div class="info-item"></div>
 <div class="info-item"></div>
</div>

Hi! I have a html code just like the above. How can I trigger click event (using jQuery) on each 'info-item' element from 'news-info' div by clicking each element from the above 'news' div? I mean: when I click the first news-item to trigger click event on the first info-item and the same for the second and the third. I tried something like that:

$( ".news" ).each(function() {

        $(this).find( "news-item" ).on('click', function() {

            $('.news-info').find('.info-item').trigger('click');

        });

    });
2
  • If I understant well, if you click on the FIRST "news-item" you want to trigger the FIRST "info-item", the SECOND "news-item" the SECOND "info-item", etc.? Commented Dec 20, 2017 at 15:17
  • yes, just like that! Commented Dec 20, 2017 at 15:21

4 Answers 4

1

You can use eq() that will allow you to select element based on his index and use index() to get the index of an element. With both functions, you can easily map the i-th element in the first container with the i-th element of the second one.

$(".news .news-item").click(function() {
  $(".news-info").find(".info-item").eq($(this).index()).trigger('click');
});


/* To test the click event */
$(".info-item").click(function() {
  $(this).css('color', 'red');
})
div {
  padding: 10px;
  border: 1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="news">
  <div class="news-item">a</div>
  <div class="news-item">b</div>
  <div class="news-item">c</div>
</div>

<div class="news-info">
  <div class="info-item">1</div>
  <div class="info-item">2</div>
  <div class="info-item">3</div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0
$( ".news" ).each(function() {

    $(this).find( "news-item" ).on('click', function() {

        $('.news-info').find('.info-item').click();

    });

});

Comments

0

If the corresponding divs sequentially match you can grab the index of the clicked .news-item via index() and find the other through eq(). From there you can trigger the click. I added a second click handler just as an example.

$('.news .news-item').on('click', function() {
  var currentIndex = $(this).index();

  $('.news-info .info-item').eq(currentIndex).trigger('click');
});

$('.news-info .info-item').on('click', function() {
  console.log("I'm " + this.textContent + " and I've been clicked or clicked through a trigger!")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="news">
  <div class="news-item">News Item 1</div>
  <div class="news-item">News Item 2</div>
  <div class="news-item">News Item 3</div>
</div>

<div class="news-info">
  <div class="info-item">Info Item 1</div>
  <div class="info-item">Info Item 2</div>
  <div class="info-item">Info Item 3</div>
</div>

Comments

0

you can do it like this

$(".news div:first").click(function() {
$( ".news-info div:first" ).trigger( "click" ); 
});

try it online https://jsfiddle.net/2w27nLos/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.