Is there a way to display the entered value below another input field, similar to the layout used in Facebook

Is there a way for me to add another comment below the printed value, similar to Facebook comments? I want the reply option to appear under the displayed value after submitting, and when I click on reply, another input box should show up so that I can comment again on that specific value just like on Facebook. Also, I would like the input value to clear after submitting.

Can someone help me with this issue?

$(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>

Answer №1

$(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);
  });
});
<!-- Updated CSS link -->
<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>

Answer №2

Doing something like this is possible-

$(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> 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble displaying a Partial View in Jquery Modal using Ajax

I've tried searching on Google and reading various blogs, but I'm facing difficulties in displaying a Partial View within a jQuery Modal using Ajax. Below is the code for the Main View: <%@ Page Title="" Language="C#" MasterPageFile="~/Views ...

Trouble with cookies in CakePHP during an AJAX request?

Currently, I am working on implementing an Ajax login feature using the auth component within my CakePHP application. Everything seems to be functioning correctly except for the "remember me" functionality. I have been attempting to set a cookie using the ...

Saving the value of an option from a view to a controller in Laravel: a step-by-step

I am seeking a way to transfer the selected option value from the view to the controller. Below is the Html code present in the view:- <label for="delivery_option" class="prod-head size-head"><b>Select Delivery Option:</b></label> ...

CSS3 :target and display:none inconsistency problem in Internet Explorer and Firefox

Creating a one-page site with navigation tabs that toggle between hidden divs using :target CSS3 selector is my current project. To ensure the first div is visible upon page load, I implemented this line of code: document.location.href = "#div1"; The HTM ...

What could be causing my Gatsby/Material-UI website to display slightly larger than the viewport?

For my personal website, I decided to use the Gatsby/MUI starter as a base. However, I am facing an issue where all pages are rendering slightly larger than the viewport size, regardless of the device screen size. The site utilizes MUI Grid and I have alr ...

Setting up button alignment in Bootstrap

In my current template, I am utilizing Bootstrap and need to position three buttons accordingly. The first button should be all the way to the left, the second in the middle, and the third (final) one on the right within a container element. While attempt ...

Are there potential security vulnerabilities associated with AJAX form validation?

In the process of form validation, I am seeking a solution that does not involve reloading the entire page. Currently, I am utilizing JavaScript for this task, but I am aware of its security vulnerabilities. To address this concern, I am considering implem ...

Tips for employing Flex-grow alongside the behavior of "justify-content: start"

I am working with a flex container setup like this: .container { display: flex; flex-flow: row wrap; justify-content: start; width: 100%; } .item { border: 1px solid red; flex: 1 1 33%; height: 100px; } <div class="container"> <d ...

What is the best way to display JSON data in a readable format?

I received a JSON file with the following structure: { "data":{ "uuid":"123", "name":"TestData", "alias":null, "created_at":"2021-03-17T11:57:29.000000Z&q ...

Error: Attempting to insert or update the "tokens" table violates the foreign key constraint "tokens_userId_fkey" in Sequelize

I am facing an issue that I can't seem to resolve, as I keep encountering an error related to a constraint violation. The tables involved in this problem are Token and User, which are linked through the userId column. The error occurs when I try to cr ...

Run an <a/> tag using code

Below are the HTML tags I am working with: <li><a href="#view2" >Photo</a></li> <li><a href="#view3">Data</a></li> <li><a href="#view4">Cloud Shop</a></li> <li><a href="#view ...

Synchronous CORS XHR encounters issues with 302 redirect, while asynchronous function successfully redirects

Why does Firefox seem to be the only browser that doesn't throw an error when performing a synchronous request? Any insights on this peculiar behavior? // Ensure your JS console is open when running this script var url = '//api.soundcloud.com/ ...

Creating a jQuery tab plugin that utilizes an external method

Here is a simple tab plugin that I developed. I am looking for suggestions on how to make it easier to switch tabs using an external method. /** * Custom jQuery Tabs Plugin */ (function($) { $.fn.customTabs = function(options) { var default ...

Double trouble: Knockout validation errors displayed twice

Currently, I am using the knockout validation plugin to validate a basic form field. The validation functionality is working as expected, however, it seems to be displaying the same error message twice under the text box. The code snippet that I am using ...

Proper method for incorporating a single database pool across an Express application

Disclaimer: This issue pertains to singleton objects in Node.js and not DB pools. While developing an Express.js application with the mysqljs node module for DB connections, I am interested in creating a single pool object that can be reused across differ ...

Updating a nested object in MongoDB using Mongoose and calling the markModified method

Regrettably, I am lacking a suitable record to carry out a test on this. Furthermore, I have been unable to locate any information related to this particular issue. Let's consider a scenario where I have a document structured as shown below: { ema ...

How can I retrieve the index of a v-for loop within a function in Vue.js HTML?

How can I splice the array from the fields in HTML Vue JS if the status is true? I also need to pass the index value to a function. Is this possible? The error I am encountering is: Uncaught ReferenceError: index is not defined at Object.success (80 ...

Dealing with Overflow in CSS DIV Elements

Link to site: Suddenly, without any changes to the css, I noticed that when I expand the "browse" categories, they overflow outside of the containing divs. Here is the css code for the divs: .bodyArea { width: 900px; background-image:url(/images/ ...

The controller argument for Ajax data object is found to be empty

Currently, I am attempting to make an ajax call to a controller method that accepts an object. The code I have at the moment is as follows: [HttpPost] public ActionResult EditStudent(Student student) { //do something } Here is the Ajax call I am us ...

Invoking Ajax Within Every Loop

I am creating dynamic HTML buttons where, upon pressing each button, I want to make an AJAX call to retrieve a value. However, I am encountering an issue where I get as many console outputs as the number of buttons pressed. Here is my PHP code: if(isset($ ...