0

I am working with bootstrap 4 modals and thymeleaf template engine. But, the data from inline JavaScript is not loading in modal.

I am sure something is wrong with the syntax in JavaScript which thymeleaf doesn't like.

To make debugging simple, I am trying to re-implement from this example from bootstrap documentations

https://getbootstrap.com/docs/4.3/components/modal/#varying-modal-content

I've tried simplest java-script function to display an alert and that works fine.

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <th:block th:fragment="header">
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport"
              content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="shortcut icon" type="image/x-icon"
              href="images/att_globe.png"/>
        <link th:href="@{/webjars/bootstrap-table/1.15.4/dist/bootstrap-table.min.css}"
              rel="stylesheet">
        <link th:href="@{/webjars/bootstrap/4.3.1/css/bootstrap.min.css}"
              rel="stylesheet">
        <link href="@{/webjars/font-awesome/5.11.2/css/all.css}" rel="stylesheet">
    </th:block>
</head>
<body>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="exampleModalLabel">New message</h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="recipient-name" class="control-label">Recipient:</label>
                        <input type="text" class="form-control" id="recipient-name">
                    </div>
                    <div class="form-group">
                        <label for="message-text" class="control-label">Message:</label>
                        <textarea class="form-control" id="message-text"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Send message</button>
            </div>
        </div>
    </div>
</div>
<script th:inline="javascript">
    /*<![CDATA[*/
        $('#exampleModal').on('show.bs.modal', function (event) {
          var button = $(event.relatedTarget) // Button that triggered the modal
          var recipient = button.data('whatever') // Extract info from data-* attributes
          // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
          // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
          var modal = $(this)
          modal.find('.modal-title').text('New message to ' + recipient)
          modal.find('.modal-body input').val(recipient)
        })
    /*]]>*/
</script>
<script th:src="@{/webjars/jquery/3.3.1/jquery.min.js}"></script>
<script th:src="@{/webjars/popper.js/1.14.7/umd/popper.min.js}"></script>
<script th:src="@{/webjars/bootstrap/4.3.1/js/bootstrap.min.js}"></script>
<script th:src="@{/webjars/bootstrap-table/1.15.4/dist/bootstrap-table.min.js}"></script>
</body>
</html>

Expected -

https://i.stack.imgur.com/tV8nM.jpg

But, this is what I am getting -

https://i.stack.imgur.com/zku2e.jpg

Ashish Verma
  • 762
  • 8
  • 14
  • Your browser console will tell you exactly what the error is... _"ReferenceError: $ is not defined"_. This is because you're trying to use jQuery **before** it's included in your page – Phil Nov 12 '19 at 00:20
  • Also, since you have no Thymeleaf expressions within your ` – Phil Nov 12 '19 at 00:22

0 Answers0