Get the index of clicked items from name on the input field inside jquer repeater

Viewed 603

I am using the jquery repeater and the input field looks like this:

$(document).ready(function () {
  $('.repeater').repeater({});
});
function getIndex(item){
    let index = $(item).index();
    console.log("index >>", index);
}
<!-- outer repeater -->
<form class="repeater">
    <div data-repeater-list="outer-list">
      <div data-repeater-item>
        <input onchange="getIndex(this)" type="text" name="text-input" value="A"/>
        <input data-repeater-delete type="button" value="Delete"/>

      </div>
    </div>
    <input data-repeater-create type="button" value="Add"/>
</form>

<script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.repeater/1.2.1/jquery.repeater.min.js"></script>

Here, the name field looks like: name="outer-list[0][text-input]". Now, I want to retrieve this first index [0]. If the name field was outer-list[1][text-input] then this index should be returned as 1. Here, is the jsfiddle

0 Answers
Related