Send input value to modal

0

I need to get the value of an input and send it to a modal bootstrap. I did a test but I do not know what I'm missing.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Gerador de assinatura</title>

    <!-- Bootstrap -->
    <link href="dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="dist/css/personalizado.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script><scriptsrc="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <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="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">

            <input type="hidden" class="form-control" id="nome-hidden" name="nome-hidden">
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
<div class="container centralizar">

    <div class="row">
    <div class="col-md-6 col-md-offset-1">
      <div class="input-group">
        <span class="input-group-addon" id="sizing-addon2">Nome</span>
        <input type="text" name="nome" class="form-control"  aria-describedby="sizing-addon2">
      </div>
    </div>
    </div>
<div class="row">
  <div class="col-md-2 col-md-offset-1">
  <button class="btn btn-lg btn-info btn-block"  class="modalAlerta" data-toggle="modal" data-target="#myModal">Gerar</button>
  </div>
</div>
</div>
<script>
$('#myModal').on('click', function(){
    var nome = $("#nome").val();
    $('#sizing-addon2').val(nome);
});
</script>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="dist/jquery1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="dist/js/bootstrap.min.js"></script>
  </body>
</html>
    
asked by anonymous 07.04.2017 / 22:14

2 answers

2

You need to add a listener in the event " show.bs.modal ", adjust your HTML to receive the value of the input (Notice the #text I entered inside .modal-body, it will receive the value of the input).

The final HMTL code looks like this:

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <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="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <p id="texto">  
        </p>
        <input type="hidden" class="form-control" id="nome-hidden" name="nome-hidden">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
<div class="container centralizar">

  <div class="row">
    <div class="col-md-6 col-md-offset-1">
      <div class="input-group">
        <span class="input-group-addon" id="sizing-addon2">Nome</span>
        <input type="text" name="nome" id="txtNome" class="form-control" aria-describedby="sizing-addon2">
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-md-2 col-md-offset-1">
      <button class="btn btn-lg btn-info btn-block" class="modalAlerta" data-toggle="modal" data-target="#myModal" id="modalTrigger">Gerar</button>
    </div>
  </div>
</div>

And JS was as follows:

;(function($) {
  $('#myModal').on('show.bs.modal', function() {
    var nome = $("#txtNome").val();
    $( '#texto' ).text( nome );
  });
}(jQuery));

If you prefer, you can take a look at this demo

The full list of events issued by the modal component can be viewed here

    
07.04.2017 / 22:54
0
$('#myModal').on('show.bs.modal', function() {
    var nome = $("#txtNome").val();
    $( '#texto' ).html( nome ); // Vc pode tentar assim..
    $( '#texto' ).append( nome ); // Ou assim vai depender da sua necessidade!
});
    
25.05.2018 / 20:56