0
<div class="owl-stage">
   <div class="owl-item active">
      <div class="item">
        <div class="slider-image"></div>
    </div>
</div>
<div class="owl-item">
    <div class="item">
        <div class="slider-image"></div>
    </div>
</div>
<div class="owl-item">
    <div class="item">
        <div class="slider-image"></div>
    </div>
</div>
<div class="owl-item">
    <div class="item">
        <video src="dummy" controls=""></video>
    </div>
</div>

<a href="">Play Video</a>

When I click on play video button I want the index of the "owl-item" with video tag, it should be 3 in this case ! how ?

2
  • clarify your question more. Commented Oct 25, 2017 at 8:07
  • @Amit how is it not clear when you read the last line? Commented Oct 25, 2017 at 8:12

3 Answers 3

2

You can use this line $(".owl-stage .owl-item video").closest(".owl-item").index(); it will return the index of owl-item in your case 3

Demo

$("a").click(function(e) {
e.preventDefault()
  var index = $(".owl-stage .owl-item video").closest(".owl-item").index();
  console.log(index)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="owl-stage">
  <div class="owl-item active">
    <div class="item">
      <div class="slider-image"></div>
    </div>
  </div>
  <div class="owl-item">
    <div class="item">
      <div class="slider-image"></div>
    </div>
  </div>
  <div class="owl-item">
    <div class="item">
      <div class="slider-image"></div>
    </div>
  </div>
  <div class="owl-item">
    <div class="item">
      <video src="dummy" controls=""></video>
    </div>
  </div>
</div>
<a href="">Play Video</a>

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

Comments

0

Here you go with a solution https://jsfiddle.net/emrsn3s3/1/

$('a').click(function(e){
  e.preventDefault();
  console.log($('video').closest('div.owl-item').index());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="owl-stage">
   <div class="owl-item active">
      <div class="item">
        <div class="slider-image"></div>
    </div>
</div>
<div class="owl-item">
    <div class="item">
        <div class="slider-image"></div>
    </div>
</div>
<div class="owl-item">
    <div class="item">
        <div class="slider-image"></div>
    </div>
</div>
<div class="owl-item">
    <div class="item">
        <video src="dummy" controls=""></video>
    </div>
</div>
<a href="">Play Video</a>

Hope this will help you.

1 Comment

The OP says When i click on play video button aka the <a>
0

Try the jQuery .index() function

For more information see https://api.jquery.com/index/

Example code for your case:

   $(".owl-item").click(function(){
      var index = $(".owl-item").index(this);

       alert("Index:  "+index);
   });

Just a reminder, indexes are zero-based. So the first one will be 0.

UPDATE

I see you want to click on the link and not on .owl-item.

In that case use the following code:

$("a").click(function(){
      var index = $(".owl-item video").closest(".owl-item").index();

      alert("Index:  "+index);
});

1 Comment

The OP wants to get the index of owl-item on <a href="">Play Video</a> click

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.