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 3344518
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:04:44+00:00 2026-05-18T01:04:44+00:00

Hey All, thanks for the help in advance. I’m trying to create and remove

  • 0

Hey All, thanks for the help in advance. I’m trying to create and remove input fields dynamically, they create just fine, but when i try to remove them it doesn’t seem to find the div thats created, and removes the div above it, and hence all the inputs, instead of the just the one.

Heres my jscript:

$("#addp").live('click' ,(function() {
    var partid = $('#partlist').val();
    var partname = $("#partlist option:selected").text();
    $('<div>').appendTo('#parts');
    $('<label for="parts" class="left">' + partname + ': </label>').appendTo('#parts'); 
    $('<input type="text" name="part[' + partid + ']">').appendTo('#parts');
    $('<input type="button" class="removepart" value="X">').appendTo('#parts');
    $('</div">').appendTo('#parts');
    numparts++;
})); 

$(".removepart").live('click', (function () {
    $(this).closest('div').remove();
}));

and HTML

<p class="edit">Parts Used</p>
<label for="parts" class="blank">Search for a Part</label>
<input type="text" name="partsearch" id="searchp_box" /> 
<input type="button" id="searchp_button" name="search" value="Search">
<br/>
<label for="parts" class="blank">Parts Used</label>
<select name="parts" id="partlist">
</select>
<input type="button" name="search" id="addp" value="Add Part">
<br>
<div id="parts">
</div>

It should removes the closest div, which is created when you press the add button, but instead it removes the whole div id=”parts” .

I searched around and was told to use .live, but it didn’t seem to work 🙁

Thanks for reading 🙂

  • 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-05-18T01:04:44+00:00Added an answer on May 18, 2026 at 1:04 am

    When jquery appends (or appendTo’s) it will try and create the DOM element, meaning this line:

    $('<div>').appendTo('#parts');
    

    Makes this html:

    <div id="parts">
    <div></div>
    </div>
    

    So everything after that line is not inside your new DIV at all, it keeps getting added afterwards. You can verify in FF/Firebug Chrome/Inspect Element IE/Developer Tools.

    What i’d do is make your new part into a string, then append that string:

        $("#addp").live('click' ,(function() {
            var partid = $('#partlist').val();
            var partname = $("#partlist option:selected").text();
            var newPart = '<div> \
                           <label for="parts" class="left">' 
                           + partname + ': </label> \
                           <input type="text" name="part[' + partid + ']"> \
                           <input type="button" class="removepart" value="X"> \
                           </div>';
            $(newPart).appendTo('#parts');
            numparts++;
        })); 
    
    $(".removepart").live('click', (function () {
        $(this).closest('div').remove();
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, thanks in advance for the help. This is my code: $(document).ready(function ()
Hey all, I was trying to create groups in the addressbook of my simulator
Hey, thanks in advance for the help. I have another pretty straight forward question.
Hey all, i am trying to create a report to show how much is
Hey all! Having a little trouble with my stack. Im trying to print each
Hey all, i am in need of some help with figuring out how to
Python: How to remove all duplicate items from a list Hey guys I have
Hey all I am trying to control the horizontal scroll of a panel box
Hey all, just a quick question (should be an easy fix I think). In
Hey all. I've been looking for a while for something that can help me

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.