How to pass the html script to css?

Asked

Viewed 434 times

0

Some sites like the w3schools, when providing some code, keep the script on the same html code page.

How can I pass everything that is in the script below to my . jsp?

<div>
    <div class="container">
        <h2>Rubricas cadastradas</h2>
        <input class="form-control" id="myInput" type="text" placeholder="Pesquisar...">
        <table class="table table-sm">
            <thead class="thead-dark">
                <tr>
                    <th>Categoria</th>
                    <th>Rubrica</th>
                    <th>Valor</th>
                    <th>Excluir</th>
                </tr>
            </thead>
            <tbody id="myTable">
                <c:forEach items="${rubricas}" var="rubrica">
                    <tr>
                        <td> --- </td>
                        <td> ${rubrica.nome}</td>
                        <td> ${rubrica.getValorTotal()}</td>
                        <td style="width: 16%">
                            <form action="excluirProjeto" method="POST">
                                <input type="hidden" class="form-control" value="${projeto.id}" name="projeto_id">
                                <button type="submit" class="btn btn-link"> <img src="../img/excluir.png" alt="Logo" style="width:100%;"> </button>
                            </form> 
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
      </div>
</div>
<script>
    $(document).ready(function(){
        $("#myInput").on("keyup", function() {
            var value = $(this).val().toLowerCase();
            $("#myTable tr").filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });
</script>
  • A question just to make sure... jsp = ?

  • @brnTwp jsp

  • What I didn’t understand was the title of the question (How to pass the html script to css?), pass to . jsp or to css?

1 answer

0


The best practice for working with scripts is to use an external file and call it within a tag script

The code below should be in a separate file. For example: script.js

$(document).ready(function(){
    $("#myInput").on("keyup", function() {
        var value = $(this).val().toLowerCase();
        $("#myTable tr").filter(function() {
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
        });
    });
});

And in his html, you call the file as follows:

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="script.js"></script>

In the Attribute "src" should contain the path to your file .js

It is also good practice to call the script tag on the last line, before closing the tag </body>

  • In my system I am using a base file that is with all the Imports including that of javascript. I tried this way putting the html script there and calling at the base but still it was not (and not even testanto with an import on that same page).

  • 1

    If you notice, the script uses jquery, probably in the code you copied from the site calls the jquery library in some way that is not visible in the code. See on the console which error appears

  • You can use a CDN that the jquery community itself provides: <script src="https://code.jquery.com/jquery-2.2.4.min.js" Integrity="sha256-Bbhdlvqf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="Anonymous"></script> This link must come BEFORE your script tag

Browser other questions tagged

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