I have some items I am storing in an element that get added at various times like this:
document.getElementById('rout_markers').value = str;
I am not too good with JavaScript, but as I understand it, the values get stored as an array, correct?
What I need to do is to be able to remove all the elements or to be able to remove the last element that was added.
How can I do that?
Thanks!
If you’re assigning
strto an element then there are no arrays involved here – you’ll be overwriting each previously-assigned value with the latest and thereby storing only the latest value.You could use an array but you’d have to know the location of each item in the array, so if you wanted to assign or nullify a specific element in your array, you’d have to a have a record of where it was – although you could get around that it with a multi-dimensioned array, where the first element at each index is the name of the property, and the second element at each index is that value of the property.
If you want to store multiple properties in a field in order to retrieve them all later, there are two simple ways of doing this.
Consider using either a field for every property.
If you do this then I’d suggest using a naming convention for the fields so that you can more easily assign the property.
Concatenating a string to form a collection of key-value pairs, very much like a query-string.
In the example you gave, this would mean storing something like:
var keyVals = 'route_markers' + '=' + str + '&';document.getElementById('myHiddenProperties').value = keyVals;When you want to assign another property to this string you do something like this:
In this way, if you want to remove a specific key-value pair, you split the stored value like this
You then have an array of key-value pairs.
If you want to retrieve a value from this array, or blank one of the values then loop through this array, splitting each into its key and value, like this: