What is the best way to transfer the text from the input box to a div at the bottom of the page?

Recently, I came across a small piece of HTML containing two input boxes, a checkbox, and an "Add" button:

<div class="row">
            <div class="form-group col-xs-4">
                <input type="text" class="form-control" id="items" name="items" placeholder="Enter item description">
            </div>
            <div class="form-group col-xs-3">
                <input type="text" class="form-control" id="quantity" name="quantity" placeholder="Enter Quantity">
            </div>
            <div class="form-group">
                <div class="form-group col-xs-3">
                    <div class="form-group">
                        <div class="checkbox">
                            <input type="checkbox" id="in-order" name="in-order">
                        </div>
                    </div>
                </div>
                <div class="form-group col-xs-2">
                    <div class="btn btn-primary" id="add-btn">Add</div>
                </div>
            </div>
        </div>

     <div id="persisted-items"></div>

I've been trying to figure out how to extract the data entered into the text boxes and display it in the bottom div "persisted-items" once the Add button is clicked. Additionally, I'd like to have a "Delete" icon or link next to each entry so I can remove them if needed.

Although I attempted a similar approach using table rows, I couldn't make much progress.

Here is a snippet of what I tried:

$('#add-btn').click(function(){
  $("#persisted-items").append($("#items").val());
  $("#persisted-items").append($("#quantity").val());
  $("#persisted-items").append($("#in-order").val());
});

If anyone has any insights or solutions on how to achieve this functionality, I would greatly appreciate the help. Thank you!

Answer №1

Check out this cool jsfiddle link: http://jsfiddle.net/8z41rmxh/ for displaying and removing information.

If you need to get rid of a node, use this handy function:

function deleteNode(nodeId) {
    document.getElementById("element"+nodeId).remove(); 
};

When showcasing user input data, don't forget to call the delete method like so:

var count = 0;
$('#add-button').click(function(){
    $("#stored-elements").append("<div id='element"+count+"'><span onclick='deleteNode("+count+")'>[ remove ]</span> "+  $("#details").val() + " - " + $("#amount").val() + " - " + $("#requirements").val() +" </div>");
    count++;
});

Answer №2

Here is a simple JavaScript solution for you:

Check out this demo on http://jsfiddle.net/UNIQUE_USER/abcdefg/123/

<input id="inputField" type="text">
<button id="submitButton" onclick="return submitForm()">SUBMIT</button>
<script>
    function submitForm() {
        var userInput = document.getElementById("inputField").value;
        if (userInput == null || userInput == ""){
            return false;
        }
        document.getElementById("submitButton").innerHTML = userInput;
    }
</script>

Answer №3

Include jquery script and ensure the click function is within the document ready block

$(document).ready(function(){
  $('#add-btn').click(function(){
    $("#persisted-items").append($("#items").val());
    $("#persisted-items").append($("#quantity").val());
    $("#persisted-items").append($("#in-order").val());
  });
});

http://plnkr.co/edit/BC3nekjJPPXrNQvRKUbI?p=preview

Answer №4

Are you in search of something similar to this?

$(document).ready(function() {
    $('#add-btn').click(function(){
        var newHtml = '<div>Items: ' + $("#items").val() + ', Quantity: ' + $("#quantity").val() + ', In Order: ' + $("#in-order").val() + ' <div class="btn btn-danger deleteBtn">Delete</div></div>' ;
        
        $("#persisted-items").append(newHtml);
        $("#items").val('');
        $("#quantity").val('');
    });
    
    $(document).on('click', '.deleteBtn', function() {
    $(this).parent('div').remove();
    });
});
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
    <div class="form-group col-xs-4">
        <input type="text" class="form-control" id="items" name="items" placeholder="Enter item description"/>
    </div>
    <div class="form-group col-xs-3">
        <input type="text" class="form-control" id="quantity" name="quantity" placeholder="Enter Quantity"/>
    </div>
    <div class="form-group">
        <div class="form-group col-xs-3">
            <div class="form-group">
                <div class="checkbox">
                    <input type="checkbox" id="in-order" name="in-order"/>
                </div>
            </div>
        </div>
        <div class="form-group col-xs-2">
            <div class="btn btn-primary" id="add-btn">Add</div>
        </div>
    </div>
</div>

<div id="persisted-items">
    
</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

"Converting domain names with punycode and utilizing Firebase for a

My current project involves updating a Firebase app with NextJS, and I've encountered an issue that needs to be resolved. Upon running the command: % npm run build I received the following message: (node:3719) [DEP0040] DeprecationWarning: The `pun ...

Excel's VBa cannot locate the table within the innerHTML of IE

My current dilemma involves attempting to extract a table from a webpage. The issue is that copying the entire page is not feasible due to buttons and dynamic elements causing a memory overload when pasted into Excel. To work around this, I am trying to ex ...

Creating responsive list items using Bootstrap 4 and Flexbox: Adjusting the width of <li> elements to fit within containers

Struggling with expanding li elements to match the container width? Despite tweaking individual widths and Flexbox properties, the desired outcome remains elusive. How can the width of each li be adjusted to align with the container, mirroring the dimensio ...

Error: Unable to find the specified "location.ejs" view in the views directory

I am encountering the following error - Error: Failed to find view "location.ejs" in views folder "e:\NodeJs_Project\geolocationProject\views" at Function.render Your help in resolving this issue would be greatly appreciated. server.js ...

It is not possible to delete a class from an element once it has been added

Issue can be replicated by visiting the following URL: Click on the hamburger icon to open the navigation menu Click on "Services" Click "< Services" within the submenu to attempt to go back For some reason, the removeClass method is not removing t ...

Tips for utilizing field mapping for a nested item in TypeScript

I am working with two separate objects below, let response = { offer: { custom_fields: { job_title: 'engineer' }, starts_at: 'test', ...

Strategies for detecting changes in multiple HTML select elements with jQuery

I currently have three select HTML tags and I would like to filter my table using the selected values (group of selected tags) with an AJAX request. For example, filtering by Gender, Location, and Season. My question is how can I achieve this without dup ...

The input type file is not correctly inserting an image into the image tag

While I was working on a project, I had a question that got answered but now I need to find a different way to use the solution. I have created a jsFiddle to demonstrate how it currently works. You can view it here: http://jsfiddle.net/TbZzH/4/ However, w ...

When a Javascript function marked as async is executed, it will return an object

Async function is returning [object Promise] instead of the desired real value. Interestingly, I can see the value in the console log. It seems like this behavior is expected from the function, but I'm unsure how to fix my code. This code snippet is ...

SQL query for finding the number of shared nodes between two given nodes

How can we determine the common nodes between two specific nodes in SQL, using the following example: Count the occurrences of <li> tags between <h2 id="vgn">VGN A </h2> and <h2 id="vgn">VGN </h2>, as well as the total number ...

When using ng-repeat in Angular.js, an additional td is created

https://jsfiddle.net/gdrkftwm/ https://i.sstatic.net/CTi2F.jpg I have encountered a problem while creating a table from a Json object. There seems to be an extra td being generated, and I'm not sure why. I want the structure of my table to resemble ...

Displaying a text in a Django template as a JSON entity

I am currently facing a challenge with an angular app that sends JSON data to a Django backend. The Django application saves the JSON data into a database and later retrieves it to send it back to the angular app. However, I am struggling to get this entir ...

Exploring the connections in Mongoose JS

I'm in the process of building an express app with a user model and a post model. Each user can have multiple posts, and each post is associated with a specific user. Below are the models I have implemented: user.js var mongoose = require('mong ...

Issue with AngularJS bug in Internet Explorer when using regular style attribute compared to ng-style

While working with Angular JS v1.1.5, I came across an interesting issue related to Internet Explorer. In IE 9, 10, 11, and Edge, the following code snippet doesn't work as expected, even though it works fine in Chrome: <div style="width: {{progr ...

Sleek Navigation in CSS and HTML

Hello, as I work on my website with multiple pages, I am looking to implement smooth scrolling on a specific page without using the html tag for the entire site. Below is the code snippet I am working with: {% if section.settings.display_product_detail_des ...

Retrieve the row from table A that corresponds to the row of buttons in table B

My current project involves using PHP and Bootstrap along with tables. In TableA, each row contains a button with a value assigned to it equal to the row number. See code snippet below: If I click on the button of the second row in TableA, I want to di ...

Show the Array List in a left-to-right format

Is there a way to display an array list from left to right with a div scroll, instead of top to bottom? I am having trouble achieving this. Here is my demo code for reference. HTML <div> <h2 class="ylet-primary-500 alignleft">Sessions</h ...

Mastering jQuery placement using CSS3 "transform: scale"

There seems to be an issue with jQuery not functioning well with CSS "transform: scale()" (however, it works fine with "transform: translate()") Please examine this straightforward example: $(document).ready(function() { $('#root').dblclic ...

Have you tried incorporating Nostramap Javascript into your vue.js projects?

Instructions on Using Nostramap API to Call a Map. Refer to the following example: ...

My regex match isn't functioning as expected when using jQuery's textcomplete feature

I have implemented a jQuery plugin(part 3) from this website to provide autocomplete functionality for text input. The current regex used for matching is: match: /\B@(\w*)$/, However, I am facing an issue where I want the autocomplete options to ...