modal call does not work (bootstrap)

Asked

Viewed 526 times

0

Good morning! I’ll rephrase the question: mount a table that receives data and a link to edit this data. So far so good.

The click on this link should open the modal for data editing. I already checked the references to Bootstrap and Jquery and I can’t understand why I still show the error message: Uncaught Typeerror: $(...). modal is not a Function

Below the snippets of my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

Edit

<div class="modal" id="modalUsuario">
            <div class="modal-dialog">
                <div class="modal-content">
                    <!-- Cabeçalho do Modal -->
                    <div class="modal-header">
                        <h3 id="myModalLabel"></h3>
                    </div>
                    <!-- Corpo do Modal -->
                    <div class="modal-body">
                        <div class="container">
                            <div class="row">               
                                <div class="col-md-12">
                                    <input class = "form-control" type="text" id="txtNomeCidade" name="txtNomeCidade" placeholder = "Nome da cidade" style="width:auto;"/><br/>                 
                                </div>
                            </div><br/>
                            <div class="row">               
                                <div class="col-md-12">                 
                                    <textarea class = "form-control" style="width:auto;" class="form-control" id="informacoes" name="informacoes" rows="5" cols="33" placeholder="Informações sobre a cidade"/></textarea><br/>
                                </div>
                            </div><br/>                    
                        </div>
                    </div>
                    <!-- Rodapé do Modal -->
                    <div class="modal-footer">
                        <div id="mensagem_modal_edita_cidade"></div>
                        <button id="btnSalvarAlteracao" class="btn btn-success">Salvar alteração</button>
                        <button class="btn btn-danger" data-dismiss="modal">Fechar</button>
                        <div class="spinner-border" role="status">
                            <span class="sr-only">Loading...</span>
                        </div>
                    </div>
                </div>
            </div>
        </div> 

<script>
            function carregarModalEdicaoUsuario(id){
               $("#modalUsuario").modal("show"); 
            };
</script>
  • You don’t need to create a script to open the modal if you use HTML correctly.

  • No need to put javascript: within the onclick. Code within this attribute is already Javascript. Just call only the function: onclick="carregarModalAlterarSenha();".

2 answers

0


You don’t need to call a function to open the modal. Just put in the tag <a> the attributes:

data-toggle="modal" data-target="#modalSenha"

In the data-target you put the modal id. Only this opens the modal:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<a href="#" data-toggle="modal" data-target="#modalSenha">Alterar senha</a>
<!-- Modal -->
<div class="modal fade" id="modalSenha" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Título do modal</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
        <button type="button" class="btn btn-primary">Salvar mudanças</button>
      </div>
    </div>
  </div>
</div>

  • Good morning Sam! I rephrased the question. Thank you.

  • Good morning. Testing here your code the modal opens without problems, even calling the function.

  • Oh yes, there must be some conflict going on so... I may be calling different versions of bootstrap and/or jquery?

0

    <a href="#" id="alterarSenha">Alterar senha</a>

    <script>
        $("#alterarSenha").click(function() {
            $("#modalSenha").modal("show"); 
        })
    </script>
  • Good morning Alessandro. I rephrased the question. Thank you.

Browser other questions tagged

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