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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:22:11+00:00 2026-06-18T15:22:11+00:00

folks! Today I created this script that has the following functionality: add new items

  • 0

folks! Today I created this script that has the following functionality:

  • add new items to array
  • list all items from the array
  • remove an item from the array

There are two functions:

  • addToFood() – adds the value of input to the array and updates
    innerHTML of div
  • removeRecord(i) – remove a record from the array and updates
    innerHTML of div

The code includes 3 for loops and you can see it at – http://jsfiddle.net/menian/3b4qp/1/

My Master told me that those 3 for loops make the solution way to heavy. Is there a better way to do the same thing? Is it better to decrease the loops and try to use splice? Thanks in advance.

HTML

<!-- we add to our foodList from the value of the following input -->    
<input type="text" value="food" id="addFood" />

<!-- we call addToFood(); through the following button  -->    
<input type="submit" value="Add more to food" onClick="addToFood();">

<!-- The list of food is displayed in the following div -->    
<div id="foods"></div>

JavaScript

var foodList = [];

function addToFood () {
    var addFood = document.getElementById('addFood').value;
    foodList.push(addFood);

    for (i = 0; i < foodList.length; i++)   {
        var newFood = "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>";
    };
    document.getElementById('foods').innerHTML += newFood;
}


function removeRecord (i) {

    // define variable j with equal to the number we got from removeRecord
    var j = i;

    // define and create a new temporary array
    var tempList = [];

    // empty newFood
    // at the end of the function we "refill" it with the new content
    var newFood = "";
    for (var i = 0; i < foodList.length; i++) {
        if(i != j) {

    // we add all records except the one == to j to the new array
    // the record eual to j is the one we've clicked on X to remove
            tempList.push(foodList[i]);
        } 
    };

    // make redefine foodList by making it equal to the tempList array
    // it should be smaller with one record
    foodList = tempList;

    // re-display the records from foodList the same way we did it in addToFood()
    for (var i = 0; i < foodList.length; i++) {
        newFood += "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>";
    };
    document.getElementById('foods').innerHTML = newFood;
}
  • 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-18T15:22:12+00:00Added an answer on June 18, 2026 at 3:22 pm

    You should use array.splice(position,nbItems)

    function removeRecord (i) {
        foodList.splice(i, 1); // remove element at position i
        var newFood = "";
        for (var i = 0; i < foodList.length; i++) {
            newFood += "<a href='#' onClick='removeRecord(" + i + ");'>X</a> "
                + foodList[i] + " <br>";
        };
        document.getElementById('foods').innerHTML = newFood;
    }
    

    http://jsfiddle.net/3b4qp/5/

    Now using JQuery:

    $(function(){
        $(document).on('click','input[type=submit]',function(){
            $('#foods')
               .append('<div><a href="#" class="item">X</a> ' 
                    + $('#addFood').val() + '</div>');
        });
    
        $(document).on('click','.item',function(){
            $(this).parent().remove();
        });
    });
    

    http://jsfiddle.net/jfWa3/

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

Sidebar

Related Questions

folks, I have a problem when I'm creating new object that has master. The
I'm new to the paypal API, I read an article today that says :
Folks, I have android application that presently has 1 warning, in line 80 of
Folks, I am developing Browser Helper Object, that has to change search assistant URL
Folks, we all know that IP blacklisting doesn't work - spammers can come in
The company I work for is wanting to add blog functionality to our website
Folks, i have the following problem. My website uses an iframe to select an
folks. I'm starting with CakePHP and after reviewing this tutorial ( http://book.cakephp.org/view/1543/Simple-Acl-controlled-Application ) and
Folks, I tired all my PHP skills to extract domain name strings from a
Folks, I've been following a ruby tutorial, at a step where I am trying

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.