jquery does not work on foreach laravel blade

0

The jquery script works if you put it out of the foreach, but qd put it inside it does not work and does not show any errors on the console:

<div class="box-indicated">
                @foreach($list_indicated as $indicated)

                        <label class="checkbox-inline pull-right">
                            <input type="checkbox" {{ $indicated['is_active'] == '1' ? "checked" : null }} data-toggle="toggle" name="deactivate{{$indicated['id']}}" id="deactivate{{$indicated['id']}}" data-width="75" data-height="25" data-offstyle="danger" data-on="Ativo" data-off="Inativo" class="email_repass_created">
                        </label>>

                <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script><script>jQuery('#deactivate{{$indicated['id']}}').change(function(){swal({title:'Aviso',html:"Dados atualizados com sucesso",
                            confirmButtonText: 'OK',
                            confirmButtonColor: '#55a07f',
                            allowOutsideClick: false,
                            type: 'success'
                        })
                        console.log('deactivate');

                    })
                </script>

                @endforeach
    
asked by anonymous 12.01.2018 / 17:41

1 answer

0

This is happening because you are importing jquery every time in the looping. Put jQuery out of the loop.

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script><divclass="box-indicated">
    @foreach($list_indicated as $indicated)

            <label class="checkbox-inline pull-right">
                <input type="checkbox" {{ $indicated['is_active'] == '1' ? "checked" : null }} data-toggle="toggle" name="deactivate{{$indicated['id']}}" id="deactivate{{$indicated['id']}}" data-width="75" data-height="25" data-offstyle="danger" data-on="Ativo" data-off="Inativo" class="email_repass_created">
            </label>>

    <script>
        jQuery('#deactivate{{$indicated['id']}}').change(function(){
            swal({
                title: 'Aviso',
                html: "Dados atualizados com sucesso",
                confirmButtonText: 'OK',
                confirmButtonColor: '#55a07f',
                allowOutsideClick: false,
                type: 'success'
            })
            console.log('deactivate');

        })
    </script>

    @endforeach
</div>

Is this the best way?

Is this way you're doing the best? Create multiple events for each input? Is not it better to create the event for an input and within these inputs you mention the id? Just a suggestion.

Is it correct how to access the attributes?

Another thing ... I can see here in the Blade Templates that they ask you to use the attributes of the item created in foreach as follows:

@foreach($list_indicated as $indicated)
    $indicated->id
@endforeach

I've never worked with Laravel and it's been years since I've even been close to PHP . So I do not know if I'm talking about some bullshit, but it's worth considering that.

    
12.01.2018 / 17:50