Enhance your HTML input form with Bootstrap by adding more options

I am currently using a form to gather user input in order to save specific information, such as instructions for a receipt. The challenge I'm facing is that I would like to have an unlimited number of fields for these instructions, instead of the fixed 3 fields displayed in the image.

My goal is to add a button at the bottom of the form that allows users to create additional input fields dynamically. Despite researching the Bootstrap documentation, I couldn't find any references to this specific feature. Most examples provided by Bootstrap involve a predefined number of input fields.

Below is a snippet of the code I've been using to generate the input fields (currently set to display 10):

<div class="col-md-4">
        <h2>Instructions</h2>

        <div class="form-group">
            <table style="width:75%">
                <tr>
                    <th><label asp-for="Instructions[0].Designation" class="control-label"></label></th>
                </tr>
                @for (int i = 0; i < 10; i++)
                {
                    <tr>
                        <td><input asp-for="Instructions[i].Designation" class="form-control" placeholder="Step @(i+1) " /></td>
                    </tr>
                    <span asp-validation-for="Instructions[i].Designation" class="text-danger"></span>

                }
            </table>
        </div>

https://i.sstatic.net/GRSM3.png

Answer №1

Below is a complete and fully functional code snippet that demonstrates how you can achieve what you are looking for:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Instructions</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>
<div class="col-md-4">
    <h2>Instructions</h2>

    <div class="form-group">
        <table id="instructionsTable" style="width:75%">
            <tr>
                <th><label asp-for="Instructions[0].Designation" class="control-label"></label></th>
            </tr>
                <tr>
                    <td><input asp-for="Instructions[i].Designation" class="form-control" placeholder="Step @(i+1)" /></td>
                </tr>
                <span asp-validation-for="Instructions[i].Designation" class="text-danger"></span>
        </table>
    </div>
    <input id="addInputBtn" type="button" value="+" />

  <script>
    $(document).ready(function(){
        $("#addInputBtn").click(function(){
            var newInput = "<tr><td><input asp-for='Instructions[i].Designation' class='form-control' placeholder='' /></td></tr>";
            $("#instructionsTable").append(newInput);
        })
    });
  </script>
</body>
</html>

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

I've been attempting to access the Marketo API, but unfortunately, I haven't had any luck

I've been attempting to make a Marketo API call, but I keep encountering this issue: net::ERR_NAME_NOT_RESOLVED Here is the code snippet I'm using for the function: var marketoAPIcallURL = 'https://182-EMG-811.mktorest.com/rest/v1/ ...

Transfer the data from the MySQL database directly into the textarea, ensuring there are no additional spaces included

Having an issue with my code: The formatting is preserved when saving to the database, but extra spaces appear in the textarea input. Screenshot: <?php $id = $_GET['id']; ?> <form id="form1" name="form1" method="post" enctype="mu ...

Tips for utilizing both noConflict and $ on one page

Can I use both noConflict and $ on the same page? <p>This is a paragraph.</p> <h1>hello</h1> <input type="button" id="=mybtn2" value="jq" onclick="myfun2();" /> <input type="button" id="mybtn" value="$" onclick="myfun(); ...

Database conditions in question

Greetings, fellow community members! I am excited to be here, although I must admit I am still a newbie when it comes to programming. Please bear with me as I ask what may be a "noob" question. Any advice or guidance from experienced members would be great ...

Ways to initiate a "clean" session upon reopening the page

As a beginner in basic programming, I am currently working on developing a webpage for managing bookings. The challenge I am facing is regarding storing the $ value and booking amount in session variables. I would like these values to be reset automaticall ...

The autocomplete feature is situated quite a bit lower than the input box

One issue I am facing in my MVC Razor page is with the autocomplete functionality. When the textbox is first used, the autocomplete results appear below the textbox (as shown in this image), but on subsequent uses, it appears correctly directly below the t ...

What is the best way to implement a partial page update using ajax in Sitecore?

I am working on a web application that includes a specific page made up of a sitecore layout and multiple sublayouts. One of these sublayouts is a user control that I need to refresh when a certain button is clicked. I want only that particular sublayout ...

Padding on the left and right in Bootstrap

Here is the code snippet I'm working with: <div class="col-md-12 pt-5 let-top"> <h1>A</h1> </div> For small and extra small screens, I am looking to include padding on both the left and right sides. Can anyone help me wit ...

Can the content inside HTML tags be susceptible to XSS attacks?

While working with Codeigniter, I have noticed that the xss_clean() function is replacing tab characters with a single space character. This behavior ends up causing issues when the content is later displayed within <pre><code></code>< ...

Bx slider - displaying partially hidden slides at the far right while utilizing a full-width carousel

Implementing a slider on my nodejs website using bx slider has been quite an experience. Below is how I achieved it: $(document).ready(function(){ $(".bxslider").bxSlider({ minSlides: 1, maxSlides: 40, slideWidth: 300, ...

Removing HTML elements with jQuery's .html() method

I have a scenario where I am storing the complete content of a <tbody> in a variable, and after some table manipulation, I need to revert back to the original content of the <tbody>. However, when I try to switch back to the original content, j ...

When the condition is fulfilled within an HTML document

I'm a novice in the world of django. Currently, I am working on creating a poll application using django. My aim is to show the message, 'you entered correct'. if selected_choice='Yellow' Below is the code snippet I am using: de ...

Modify the css within a Bootbox dialog box

I am looking to enhance the appearance of the text "Hello world" in the following code snippet by applying some styling such as making it bold: bootbox.alert("Hello world!", function() { Example.show("Hello world callback"); }); Your help is greatly a ...

Using jQuery and AJAX to fetch the return value of a PHP class method

Starting a new project for myself has been quite the learning experience. As someone who isn't the most skilled programmer, I decided to kick things off in PHP. However, I quickly realized that executing PHP functions and class methods through HTML bu ...

Confirmation of Entity Framework Code First Insertion

I have a question regarding data insertion using DetailsView. I have a function that works fine: public Void InsertIntoTblItem(TblItem tblItem) { orderDBContext.TblItem.Add(tblItem); orderDBContext.SaveChanges(); } However, I want to enhance it b ...

Tips for preventing page breaks (when printing or saving as a PDF) in lengthy HTML tables

Here is the link to a single HTML file (including style and scripts): FQ.html The problem I'm facing can be seen in this image: https://i.sstatic.net/Nr4BZ.png I've tried several solutions, the latest of which involves the following CSS... @me ...

I'm looking for a button that, when clicked, will first verify the password. If the password is "abcd," it will display another submit button. If not, it

<form action="#" method="post" target="_blank"> <div id = "another sub"> </br> <input type = "password" size = "25px" id="pswd"> </br> </div> <button><a href="tabl ...

Incorporate a dynamic opacity effect to vertical scrolling text

Currently, I am working on the following CSS: figure p { opacity:0; } figure:hover p { opacity:1; -webkit-transition: opacity 0.35s; transition: opacity 0.35s; margin: 0; line-height: 50px; text-align: center; /* Starting position */ -moz-transfor ...

Unable to display jQuery modal due to error "Uncaught DOMException: Failed to execute 'appendChild' on 'Node': The new child element contains the parent."

Check out the JS Fiddle demo here. Here is the code in question: $('#infoTable').click(infoModal); function infoModal(){ var table = "THIS IS A TABLE"; $("#dialogContainer").dialog({ appendTo: "#msg-dialog", autoOpen: f ...

The class 'System.Web.HttpInputStream' does not have the serializable attribute applied

I am encountering an issue while attempting to upload a byte[] to Azure blob storage, resulting in the following exception: Error: Type 'System.Web.HttpInputStream' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyTok ...