Jquery not detecting new elements added by Jquery Repeater

827 Views Asked by At

Im using a jquery repeater to repeat input fields and i need to find the name of the last input field added (with the a specific id).

How im doing it :

    $('#addElements').on('click', function() {
        var nameOfLastInput = $("#inputID:last").attr("name");
        console.log(nameOfLastInput);
    });

What i get is the name of the first field every time: enter image description here

Full working code (needs a js repeater lib):

    @section('content')
    <div  id="kt_repeater_1" style="padding-left: 15px; padding-right: 15px">
        <div data-repeater-list="repeat">
            <div data-repeater-item>
                <input type="text" name="text-input" id="inputID"/>
                <input data-repeater-delete type="button" value="Delete"/>
            </div>
        </div>
        <input data-repeater-create type="button" value="Add" id="addElements"/>
    </div>

    @section('scripts')
    <script src="assets/js/jquery.repeater.js" defer></script>


    <script>

        $('#addElements').on('click', function() {
            var nameOfLastInput = $("#inputID:last").attr("name");
            console.log(nameOfLastInput);
        });
    </script>
    <script src="assets/js/jquery.repeater.min.js" defer></script>
    @endsection
@endsection
0

There are 0 best solutions below