Search code examples
javascripthtmllaravelbootbox

Bootbox alert doesn't show up in Laravel view


I'm tryin to use a bootbox confirm alert in my Laravel view which looks better than the standard js one, but the alert doesn't show up, i have popper, bootstrap and bootbox in the scripts

this is the code fragment, the whole view is a lot bigger but this is a miniature version of it to make it more readable

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/bootbox@5.1.2/dist/bootbox.all.min.js"></script>


<table>
<tr>
<th>ID</th>
<th>Action</th>
</tr>
@foreach($etablissements as $etablissement)
<tr>
<td>{{$etablissement->id}}</td>
<td>
<form action="{{ route('etablissements.destroy', $etablissement->id) }}" method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <button type='submit' name ="s"></button>
</form>
</td>
</tr>
@endforeach
</table>


<script type="text/javascript">
    var aTags = document.getElementsByTagName("s");
    for (var i=0;i<aTags.length;i++){
        addEventListener(aTags[i], 'click', function(){
          bootbox.confirm({
    message: "This is a confirm with custom button text and color! Do you like it?",
    buttons: {
        confirm: {
            label: 'Yes',
            className: 'btn-success'
        },
        cancel: {
            label: 'No',
            className: 'btn-danger'
        }
    },
    callback: function (result) {
        console.log('This was logged in the callback: ' + result);
    }
});
        });
            }
</script>

Solution

  • I haven't used bootbox, but from your JS couple of things that I've noticed and you would need to change are:

    var aTags = document.getElementsByTagName("s");
    

    The s is not the tag name, but the element name, so change that to this:

    var aTags = document.getElementsByName("s");
    

    Then to attach the click event listener to each button, change it to this:

    aTags[i].addEventListener('click', function(e){
        e.preventDefault(); // this will prevent the default action of the button
        ...
    

    Once you get this done, make sure that you have your browser console opened to check for any errors.