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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:11:51+00:00 2026-05-20T14:11:51+00:00

this looks simple enough but I just can’t get it to work. I could

  • 0

this looks simple enough but I just can’t get it to work. I could add DOM elements but I just can’t remove them when using an array.

<script language="javascript">
fields = 0;
count = 0;
function addInput() {
  if (fields != 10) {
      var htmlText =  "<input type='search' value='' name='field[]' />";
      var remButton = "<input type='button' value='del' onclick='remove()' />";
      var newElement = document.createElement('div');

      newElement.id = 'SomeID'+fields; 
      newElement.innerHTML = htmlText + remButton + newElement.id;

      var fieldsArea = document.getElementById('text');
      fieldsArea.appendChild(newElement);

      fields += 1;
  } else {
     ...
  }
count++;
}

// NEED HELP FROM HERE PLEASE
// You don't need to Loop, just get the button's id and remove that entire 'SomeID'


function remove() {
  fieldsArea = document.getElementById('text');


  fieldsArea.removeChild(SomeID1);   <-----------------------THIS WORKS!
  fieldsArea.removeChild(SomeID+count); <------------------THIS JUST WOULDN'T

  count--;
}
</script>

In the remove function, writing SomeID1 works and delete the first added element but when I try to use a ‘count’, I just can’t delete my ‘elements’.

Any help would be most appreciated.

Thank you!

  • 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-20T14:11:52+00:00Added an answer on May 20, 2026 at 2:11 pm

    You have to get a reference to the element first. Currently you are passing an undefined variable SomeID to the function.

    E.g.:

    var element = document.getElementById('SomeID' + fields);
    // or starting by zero: var element = document.getElementById('SomeID0');
    element.parentNode.removeChild(element);
    

    If you want to remove the div for which the button was clicked, you have to pass a reference to the corresponding div to the remove function.

    '<input type="button" value="del" onclick="remove(this.parentNode)" />';
    

    this will refer to the button and as it is a child of the div, this.parentNode refers to that div.

    You also have to change your function to accept the element that should be removed:

    function remove(element) {
      element.parentNode.removeChild(element);   
      count--;
    }
    

    You probably also have to update fields, but I’m not sure how your code is supposed to work.


    If you want to remove all of them you have to loop:

    for(;fields--;) {
       var element = document.getElementById('SomeID' + fields);
       element.parentNode.removeChild(element);
    }
    

    Also have a look at the documentation of removeChild.

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

Sidebar

Related Questions

I just cannot figure this out, it looks really simple but I'm relatively new
I have read several tutorials, but I just can't this to work. My apache
This looks simple but I am confused: The way I create a vector of
I have a simple app where you can login it looks like this: {{#view
I think this is a simple SQL query problem, but I am just not
I have a simple form that looks like this: <%= simple_form_for @study,:url => studies_path,
I have a simple Controller that looks like this:- @Controller @RequestMapping(value = /groups) public
I need to parse a simple DSL which looks like this: funcA Type1 a
If I have a simple web page and script that looks like this: <body>
Let's say I have a very simple PrototypeJS class that looks like this: var

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.