Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:26:09+00:00 2026-06-18T05:26:09+00:00

I have a function to add and remove a field but the remove function

  • 0

I have a function to add and remove a field but the remove function doesnt work somehow.

HTML:

<div id="parts">
    Part
    <input type="text" id="auto_part" name="auto_part" />
                <br />
    Description
    <input type="text" id="auto_description" name="auto_description" />
                <br />
</div>
    <a href="#" id="addField">Add another part</a>

jQuery:

$(function() {
var scntDiv = $('#parts');
var i = $('#parts input').size();

$('#addField').on('click', function() {
    $('<br /><div id="parts"><span>Part</span> <input type="text" id="auto_part'+i+'" name="auto_part'+i+'" /><br />').appendTo(scntDiv);
    $('<span>Description</span> <input type="text" id="auto_description'+i+'" name="auto_description'+i+'" /> <br />').appendTo(scntDiv);
    $('<input type="hidden" id="row_count" name="row_count" value="" />').appendTo(scntDiv);
    $('<a href="#" id="removefield">Remove</a></div>').appendTo(scntDiv);
    i++;
    return false;
});

$('#removefield').on('click', function() { 
    if( i > 2 ) {
            $(this).parents('div').remove();
            i--;
    }
    return false;
});
});

The problem must have to do with this line:

$('#removefield').on('click', function() {

It doesnt pass that condition.
When I click on Remove it doesnt do anything at all it just scrolls to the top.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:26:10+00:00Added an answer on June 18, 2026 at 5:26 am

    You are binding the click handler to the elements that are present in the DOM. But, your #removefield element is being dynamically added. So, the event handler is not attached to it.

    You can use .on() to use event delegation and handle also future elements. Also, you may want to use classnames instead of and id attributes. id attributes need to be unique, but you can set the classname to as many elements as you want.

    <a href="#" class="removefield">Remove</a>
    

    $("#parts").on("click", ".removefield", function() { 
      /* ... */
    });
    

    The reason why your "Remove" link doesn’t work is because you are adding the dynamic <div> element by parts hence making it invalid markup. You should be adding it all together at once. For example,

    $('#addField').on('click', function () {
        var part = '<div id="parts' + i + '"><span>Part</span> <input type="text" id="auto_part' + i + '" name="auto_part' + i + '" /><br/>' +
            '<span>Description</span> <input type="text" id="auto_description' + i + '" name="auto_description' + i + '" /> <br />' +
            '<input type="hidden" id="row_count' + i + '" name="row_count' + i + '" value="" />' +
            '<a href="#" class="removefield' + i + '">Remove</a></div>';
        scntDiv.after(part);
        i++;
        return false;
    });
    

    $(document).on("click", ".removefield", function() {
        if( i > 2 ) {
            $(this).parent('div').remove();
            i--;
        }
        return false;
    });
    

    You can see it here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code like this $(document).ready(function(){ $(.add).bind ('click', function () { $('.main').append('<div class=add>Add
I have dynamic form like: <form id=formaa> <div class=row> <input type=text name=item class=item></input> <input
I have this function // add history paths and save data function AddPath( strTag,
I have written a function for deleting object preiodically. function add(variable, expireSecond){ this.list[variable] =
In my MVC application in Controller i have following function to add and focus
i have made a function to add new upload and description inputs onClick. How
I have a function where I can add articles and users can comment on
I have a setup function that runs onload to add some behaviours to elements.
I have an onclick function that I want to add an anchor to the
I have a onclick function like this onclick = function(){alert('hello')} I want to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.