2

It may seem like an odd request, though I am not too good with JQuery/Javascript and there is no documentation on how to do this certain thing a customer needed.

I have this array: ["Value1", "Value2", "Value3","Value4"] and he wants it so that if he clicks on a button to delete e.g. value2, value3 and 4 are also deleted (all elements after) and for html to be changed on each one (so I can not just delete everything after, as html has to be changed in the process too).

I'm familiar with .each but it being used in such a situation seems rather slow and given JQuery's popularity, I would assume there is a better alternative or at least a method of employment of .each than looping through them all and checking their keys.

1 Answer 1

1

Here is a working jsFiddle: http://jsfiddle.net/aT3X8/1/

Given:

var arrMyArray = ["Value1", "Value2", "Value3","Value4"];

First, get the index of the item the user is deleting. Say the user deletes the 2nd item (or whichever item is deleted will need to be passed into the indexOf in place of Value3 so that we can get it's index:

var intIndex = arrMyArray.indexOf("Value2");

Now, loop thru the array, starting at the element they chose to delete:

for(i = intIndex; i < arrMyArray.length; i++){
    alert("I'm on item: " + arrMyArray[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

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.