how can i show input value under another input value like facebook comment?

Viewed 42

how can I create another comment in the printed value like as facebook comment.I want when the value will display after submitting under that value reply option will be show and when i click on reply the another input box will show, so that I would comment again on that printed value as like facebook. And I want the input value will clear after submitting.

can any one help me find this problem?

$(document).ready(function(){
  $("#btn").click(function(){
    let getVal = $("#inputValue").val();
    let img = `<img src="https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="rounded-circle" height="30px" width="30px">`
    $("#output").html($("#output").html() + " " + img + getVal + `<br>` );
  });
});
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>


<div id="main">
  <div id="output"></div>    
    <input id="inputValue" type="text" name="text">
  <button id="btn" class="btn btn-primary">display value</button>
</div>

2 Answers

It can be done with something like this-

$(document).ready(function(){
  $("#btn").click(function(){
    let getVal = $("#inputValue").val();
    let img = `<img src="https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="rounded-circle" height="30px" width="30px">`
    let replyInput = '<div class="ml-5"><input type="text" id="replyText">&nbsp;&nbsp;<button class="btn btn-primary" type="button" id="reply">reply</button></div>'
    $("#output").html($("#output").html() + " " + img + getVal + `<br>`+ replyInput );
  });
});
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>


<div id="main">
  <div class="m-5" id="output"></div>    
    <input id="inputValue" type="text" name="text">
  <button id="btn" class="btn btn-primary">display value</button>
</div>

$(document).ready(function(){
  var replyHtml = "<br><input type='button' class='reply' value='reply'><div style='display:none;'><input type='text' placeholder='reply'><input type='button' class='rep-submit' value='reply'></div>";
  
  $("#btn").click(function(){
    let getVal = $("#inputValue").val() + replyHtml;
    let img = `<img src="https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="rounded-circle" height="30px" width="30px">`
    $("#output").html($("#output").html() + " " + img + getVal + `<br>` );
    $("#inputValue").val('')
  });
  
  $(document).on('click', '.reply', function(){
    $(this).next().show();
    $(this).remove();
  });
  
  $(document).on('click', '.rep-submit', function(){
    var reply = $(this).prev().val();
    $(this).closest('div').html(reply + replyHtml);
  });
});
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>


<div id="main">
  <div id="output"></div>    
    <input id="inputValue" type="text" name="text">
  <button id="btn" class="btn btn-primary">display value</button>
</div>

Related