Send input value to modal

Asked

Viewed 1,751 times

0

I need to take the value of an input and send it to a bootstrap modal. I did a test but I don’t 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>
      <script src="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>

2 answers

2

You need to add a Istener to the event "show.bs.modal", adjust your HTML to receive the input value (Note the #text you entered inside . modal-body, it will receive the input value).

The final HMTL code is as follows:

<!-- 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 the 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 peek at this demo

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

  • 1

    Perfect, solved my problem, thank you so much for your help.

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!
});

Browser other questions tagged

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