Instructions for adding and deleting input text boxes on an ASP.NET master page

*I am currently facing an issue with adding and removing input textboxes for a CV form using ASP.NET in a master page. Whenever I click on the icon, it doesn't seem to work as intended. The idea is to have a textbox and an icon "+" to add more textboxes. Clicking on the icon should duplicate the textbox input. Additionally, there should be another icon to remove the extra textboxes.

It's important to note that I'm not utilizing MVC for this task. If there are alternative ways to achieve this functionality through coding, your insights would be greatly appreciated.

Below is a snippet of my code: enter code here (this includes JavaScript, CSS, and HTML)

$(document).ready(function () {

    var counter = 2;

    $("#addButton").click(function () {

        if (counter > 10) {
            alert("Only 10 textboxes allowed");
            return false;
        }

        var newTextBoxDiv = $(document.createElement('span'))
            .attr("id", 'TextBoxDiv' + counter);

        newTextBoxDiv.after().aspx('Textbox' + counter +
            '<input type="text" name="courseName' + counter +
            '" id="courseId' + counter + '" value="" >');

        newTextBoxDiv.appendTo("#course");

        counter++;
    });

    $("#removeButton").click(function () {
        if (counter == 1) {
            alert("No more textboxes to remove");
            return false;
        }

        counter--;

        $("#TextBoxDiv" + counter).remove();

    });

    $("#sumbitcvId").click(function () {

        var msg = '';
        for (i = 1; i < counter; i++) {
            msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
        }
        alert(msg);
    });
});
.coursecv,.mosdcv {
    font-family: 'Assistant', sans-serif;
    padding: 10px;
    margin: 10px;
    width: 27.5%;
    position: relative;
    font-size: 14px;
    color: #555;
    background-color: #fff;
    border: 1px solid #ccc;
    display: inline-block;
    }
    <div id="course">
            <div>          

            <h1>course</h1>
        
            </div>
        <span id="TextBoxDiv">
        
         <asp:TextBox ID="courseId" runat="server" name="courseName" type="Text" placeholder="course name" CssClass="coursecv" value="" required="required"></asp:TextBox>
        </span>
        <span>
        <asp:TextBox ID="mosdId" runat="server" name="mosdName" type="Text" placeholder="Corporation" CssClass="mosdcv" required="required"></asp:TextBox>
        </span>
</div>

            <div Id="ADD"  style="margin-left:87%;color: #3c6bf4;font-family: 'Assistant', sans-serif;font-size: 17px;">
                        <a href="javascript:void(0);" class="addButton" title="Add field"><i class="fas fa-plus-circle"></i></a>
                         <a href="javascript:void(0);" class="removeButton" title="Remove field"><i class="fas fa-minus-circle"></i></a>

            
</div>

Answer №1

When using JQuery to identify fields based on class names, it's important to use the dot (.) as the identifier rather than the hash (#). The hash symbol is typically used for identifying elements by their Id.

$(".addButton").click(function () {

    if (counter > 10) {
        alert("Only 10 textboxes are allowed");
        return false;
    }

    var newTextBoxDiv = $(document.createElement('span'))
        .attr("id", 'TextBoxDiv' + counter);

    newTextBoxDiv.after().append('Textbox' + counter +
        '<input type="text" name="courseName' + counter +
        '" id="courseId' + counter + '" value="" >');

    newTextBoxDiv.appendTo("#course");


    counter++;
});

The code above will execute the click action and add text boxes accordingly.

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

The overlappingmarkerspidifier is throwing an error because it is unable to access the property '__e3_' of an undefined variable

I am attempting to implement the overlapping marker spidifier feature in my code. I followed the instructions provided in this link: functioning code of oms However, upon creating the OverlappingMarkerSpiderfier object, I encounter the error: Uncaught Typ ...

NodeJS: Issue with Route is disrupting the functionality of the Restful API

Struggling to develop an API using Node.js and Express, encountering routing issues with express.Router(). Take a look at my code below: Server.js file contents: // Get necessary packages var express = require('express'); var app = express(); ...

Interactive HTML/CSS Periodic Table with Dynamic Design

Recently, I have been immersing myself in learning about responsive design in HTML/CSS and decided to challenge myself by coding the periodic table of elements using HTML/CSS to hone my skills. While I have set up the basic structure of the table with div ...

What is the best way to retrieve the specific property from a bound function?

I'm looking to retrieve the value of a specific property from a function that has already been bound. function foo(){ console.log(this.a) } const bar = foo.bind({a:1}) bar() // Outputs 1 bar.getThis() // expected result is { a: 1 } Given the code ...

Using jQuery to create radial-gradients with the background-css feature

I'm a newcomer to this online community and English is not my first language. Despite that, I will do my utmost to clearly explain the issue at hand. Recently, I utilized the .css function in jQuery to create a design element successfully. However, I ...

Clicking on ng-click can lead to the dissociation of the Angular factory

My situation involves a factory with a series of prototypes that need to call each other. The issue arises when the reference to "this" is mistakenly applied to the html template instead of the original factory class when using ng-click. For example: ang ...

Interactive jQuery Dropdown Menu with Multiple Divs

I have been working on this and I'm almost there, but the jQuery part is driving me crazy (I'm not very proficient in it yet, but I am learning). Here's the link to the fiddle: http://jsfiddle.net/5CRA5/4/ Since I can't demonstrate th ...

What is the time stamp format of 1651928421543667000?

Recently, I have encountered an issue with an API returning a timestamp as 1651928421543667000. Despite trying various PHP functions like strtotime(), datetime(), and strftime(), I am unable to find the correct format for it. Can anyone provide some guid ...

Increase the totalAmount by adding the product each time

Can someone help me understand why the totalAmount shows as 20 when I add a product? Also, why doesn't it increase when I try to increment it? Any insights would be appreciated. Thank you. ts.file productList = [ { id: 1, name: 'Louis ...

Tips for arranging information in ng-repeat using nested objects within JSON data on AngularJS

Looking to display an array of object properties in an Angular view. Here is the object: $scope._Json = { "foo": { "ItemDimension1": { "Item": "item1", "ItemIndex": 1, "SelectedItems": [{ "C ...

Transforming the API response

After making an Ajax call, the response received is: console.log(data); {"valid":true,"when":"Today"} Attempting to read the response like this: var res = data.valid; console.log(res); results in 'undefined' being displayed. To address this i ...

Issue of displaying buttons based on sibling's height under certain conditions

OBJECTIVE I have undertaken a project to enhance my skills in React and TypeScript by developing a UI chat interface. The design requirement is that when a chat message has enough vertical space, its action buttons should appear stacked vertically to the ...

Locate specific phrases within the text and conceal the corresponding lines

I have a JavaScript function that loops through each line. I want it to search for specific text on each line and hide the entire line if it contains that text. For example: <input id="search" type="button" value="Run" /> <textarea id ...

Leveraging body-parser for capturing an indefinite amount of text fields in node/express

Background In pursuit of my Free Code Camp back end certification, I am embarking on the task of creating a voting application. For detailed information and user stories required for this project, visit this link: Among the user stories is the ability to ...

Why is it that the reduce function is not returning the object I expected?

[['id', '1111'], ['name', 'aaaaa']] I came across this list. { id: '1111', name: 'aaaa' } Now, I would like to transform the list into a different format. My attempt to convert the list into t ...

Stop the erratic movement of elements in Chrome

Visit the following link: . Upon loading the page in Chrome, there seems to be a slight movement of every other element between the title and lyrics (including key selector and links). The same issue occurs when hovering over these sections. It appears to ...

Next.js server component allows for the modification of search parameters without causing a re-fetch of the data

I have a situation where I need to store form values in the URL to prevent data loss when the page is accidentally refreshed. Here's how I am currently handling it: // Form.tsx "use client" export default function Form(){ const pathname ...

Exploring the Terrain: Troubleshooting Meteor CSS with Twitter Bootstrap

Recently, I have encountered an issue that perplexes me - I am unable to debug less code in my meteor app when the twbs:boostrap package is present. Interestingly, everything works fine when I remove it, but as soon as I add it back, the CSS seems to be co ...

Troubleshooting an issue with importing a Component in ReactJS using material-ui

Using the material-ui library, I attempted to create a Table following the code provided in the Custom Table Pagination Action example. However, I encountered the following error: Error Encountered: Warning: React.createElement: type is invalid -- expect ...

Is it possible for AngularJS to share a service among multiple ng-apps?

I've been working on developing a web app that involves multiple Angular ng-apps, and I want to use the same service code for at least two of them (even though they don't share data but perform similar functions). How can I prevent code duplicati ...