-1

I want to find the index .col in .row When I click on .option it should give me the position of .col in .row

<div class=“row”>
    <div class=“col”>
        <div class=“option”>click (return 0)</div>
    </div>
 </div>
 <div class=“row”>
     <div class=“col”>
         <div class=“option”>click 2 (return 1)</div>
     </div>
</div>

Can anyone tell me how?

Leng D
  • 101
  • 1
  • 1
  • 7
  • Possible Duplicate of [Get index of clicked element in collection with jQuery](https://stackoverflow.com/q/5545283/2025923) – Tushar Feb 12 '18 at 09:49
  • Possible duplicate of [Get index of clicked element in collection with jQuery](https://stackoverflow.com/questions/5545283/get-index-of-clicked-element-in-collection-with-jquery) – Andreas Feb 12 '18 at 09:50
  • 1
    The second one will not return 1 in case of your markup. Please show us your efforts and for the sake of all of us, please change your quotes. – Ionut Necula Feb 12 '18 at 09:50
  • You want the index of the "clicked" `.row`? At least that would match the "expected" values in the markup – Andreas Feb 12 '18 at 09:53
  • @Ionut That would match the "expected" result mentioned in the markup. But you're right. It was just an assumption of mine, hence my question for clarification. – Andreas Feb 12 '18 at 09:55
  • @Andreas, ok. Anyway, the OP's question seems unclear/missing code. – Ionut Necula Feb 12 '18 at 09:56

2 Answers2

0

Put a click listener for it

$('.option').click(function (event) {
    alert($(this).index()); // the index was $(this).index()
});
Kasnady
  • 2,249
  • 3
  • 25
  • 34
0
<div class=“row”>
  <div class=“col”>
    <div class=“option” onclick="myFunction(0)">click (return 0)</div>
  </div>
</div>
<div class=“row”>
  <div class=“col”>
    <div class=“option” onclick="myFunction(1)">click 2 (return 1)</div>
  </div>
</div>
FrontTheMachine
  • 526
  • 3
  • 14