1

I have the following code which when a button is clicked checks to see the previous value stored and concatenates it with the new value - essentially to get a long list of latitudes and longitudes.

(arrayvalues is declared globally)

var previousvalues = document.getElementsByName('arrayvalues')[0].value

arrayvalues = previousvalues + latitude+ "," + longitude+"##";
//alert(arrayvalues);
document.getElementById("arrayvalues").value = arrayvalues;

The document.getElementById stores the new value here:

<input type="text" value="" name="arrayvalues" id="arrayvalues" />

However the issue I am having is the user can at any point undo their last click meaning the last latitude and longitude values stored need removing. How can I go about doing this the way I have coded it?

3
  • 1
    from your code it looks like you have a string moreso than an array. Commented Jan 28, 2015 at 17:10
  • 2
    Protip: I recommend not storing application state in HTML elements. Store these values in JavaScript and update them from HTML when needed. Someday you're going to want to access these values or change them and the HTML wont be around, and then you'll have to load the elements just to get at the state because you have 10K lines of code assuming state in DOM. Commented Jan 28, 2015 at 17:10
  • You should store the lat/long in a data structure instead of a string, such as an array, which holds objects contacting each lat/long pair. Then when you output the values to the user, you simply combine the values into the string you want the user to see. Commented Jan 28, 2015 at 17:11

1 Answer 1

1

Use the pop method available for JavaScript arrays, which will remove the last stored element. Here's a brief tutorial on pop():

So if your array is the following:

var coords = [[lat1,long1],[lat2,long2],[lat3,long3]]

Running coords.pop() will result in coords being the following:

 [[lat1,long1],[lat2,long2]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - took a bit of tweaking of my initial code but got there eventually

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.