danielpr
danielpr

Reputation: 97

Textarea after clicking button inside modal bootstrap

I have a simple modal where i want to add a textarea in the modal, But I want the textarea to be visible after clicking a button under a text inside the modal too.

fiddle here : http://jsfiddle.net/nyderetna/WV5e7/344/

HTML:

<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg launch-modal" data-toggle="modal" data-target="#myModal">
  Launch modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
      <p>Some text here</p>
      <div>another foo text</div>
      <textarea hidden id="textarea-message"></textarea>
        <button class="btn btn-reply btn-sm btn-success pull-right">Reply that foo!</button>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->

js:

$('#myModal').on('shown.bs.modal', function () {
    $(document).on('click', '.btn-reply', function () { 
    var text = $('#textarea-message').val();
    var html = '<input type="textarea" name="message" placeholder="Enter Sandman message ..." class="form-control" id="textarea-message" autocomplete="off" maxlength="200">';

    $(document).find('#textarea-message').append(html);
        });
    $('#textarea-message').show();
})

$(document).find('#textarea-message').on('keypress', function(e){

    if ( e.which == 13 ) {
        e.preventDefault();
        $('.btn-reply').trigger('click');
    }

});

CSS:

.launch-modal {
    margin: 20px;
}

Can someone help me solve this problem?

EDITED

As explained before, I forgot to tell that I want the input text will show after pressing Enter. I also changed the textarea to text.

Updated code here

Upvotes: 1

Views: 2955

Answers (1)

Udhay Titus
Udhay Titus

Reputation: 5869

I updated your fiddle check here

just give id for button and use button click event it will works

$('#btnReply').click(function () {    
    $('#textarea-message').removeClass('hide');
});

$('#myModal').on('shown.bs.modal', function () {

	$(document).on('click', '.btn-reply', function () { 
    var text = $('#textarea-message').val();
    var html = '<input type="textarea" name="message" placeholder="Enter Sandman message ..." class="form-control" id="textarea-message" autocomplete="off" maxlength="200">';
   
});

});
$('#btnShowModal').click(function () {
	$('#textarea-message').addClass('hide');
});
$('#btnReply').click(function () {

	$('#textarea-message').removeClass('hide');
});
.launch-modal {
    margin: 20px;
}
<html lang="en">
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<!-- Button trigger modal -->
<button  id="btnShowModal" class="btn btn-primary btn-lg launch-modal" data-toggle="modal" data-target="#myModal">
  Launch modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
      <p>Some text here</p>
      <div>another foo text</div>
      <textarea class="hide" id="textarea-message"></textarea>
        <button id="btnReply" class="btn btn-reply btn-sm btn-success pull-right">Reply that foo!</button>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

</body>
</html>

Upvotes: 1

Related Questions