1

I need a user to be able to perform these actions:

  1. Click an "Add" button. This will add a JavaScript object to either an array, or another JavaScript object.
  2. Click a dynamic "Remove" button. This will remove the JavaScript object from the array, or object, whose ID matches the button click.
  3. Submit the complete array, or object, of desired objects server-side for processing.

Using an array, am I able to get a handle via ID on a specific element and remove it with a "pop"-like function? If not, am I able to do so using a JavaScript object?

This question is related to this post, but there isn't much context in the original to answer my question. Thanks!

2
  • 1
    By the way, there's no such thing as a "JSON object"; you mean a Javascript object. JSON is just a format used to serialize an object to a string. Commented Nov 11, 2009 at 22:32
  • Thank you for the clarification. I have edited the post based on all of your comments and have voted you both up for getting me on the right track and using the right terminology. Commented Nov 11, 2009 at 22:56

2 Answers 2

4

You can append an element to an array using the push method, like this:

var someArray = [];    //Start with an empty array

//Later,
someArray.push({ name: 'value' });
Sign up to request clarification or add additional context in comments.

Comments

1

It may actually be better to use an object rather than an array. This will allow you to name each object you send back to the server.

var dataObject = {};

function addData(name, data1, data2, data3) {
    dataObject[name] = {
        data1: data1,
        data2: data2,
        data3: data3        
    };
}

function removeData(name) {
    delete dataObject[name];
}

2 Comments

That depends on what he's doing.
"may" being the key word in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.