jquery does not work on foreach Blade Standard

Asked

Viewed 302 times

0

jquery script works if put out of foreach, but qd put in it does not work and shows no error in 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
  • Guy doesn’t make sense you have a function for each item, imagine that this loop will have 100 records, 100 repeated functions to do the same thing.

  • 0&#xA;votar contra&#xA;favorita&#xA;o script do jquery funciona se colocar fora do foreach, mas qd coloco dentro ele não funciona e não mostra nenhum erro no console: it has no change in the element and so does not run the console and everything that is within that function ... and also could be done differently created a function and just call there with a line (although it seems kind of strange what you want to do), could explain what you really want to do, maybe there are different paths to your problem

1 answer

0

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

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<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>
        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>

That’s the best way?

Is this way you’re doing the best? Create multiple events for each input? Isn’t it better to create the event for an input and within those inputs you mention the id? Just a hint.

Correct way to access attributes?

Another thing... I see here on 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 never got to work with Laravel and I haven’t been anywhere near PHP. So I don’t know if I’m talking some bullshit, but it’s worth considering this detail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.