Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
97
rated 0 times [  98] [ 1]  / answers: 1 / hits: 38331  / 14 Years ago, thu, march 3, 2011, 12:00:00



I am attempting to remove a value from an array using splice. starting at 0 and ending at 0 splice, but it is not removing the value at index 0. I added a function getItemRow to check the species index which returns 0. I dumped the values of the array into an alert and it still outputs species which should of been deleted. invalidElement.splice(indexValue, indexValue); works as expected for indexes that are NOT 0. Why is this happening and how do I delete the value that has 0 index?



javascript code:



var invalidElement = new Array(species, alias, gender, breeding, birth_date);

//This function will be removed once fixed!!
function getItemRow()
{
var myPosition=-1
for (i=0;i<invalidElement.length;i++)
{
if(invalidElement[i]==species) {
myPosition = i;
break;
}
}
alert(myPosition)
}

function validateElement(formId, element, selector, errorContainer)
{
getItemRow()//for testing purposes
//var indexValue = $.inArray(element, invalidElement);
var indexValue = invalidElement.indexOf(element);

alert(element);
$.ajax({
type: 'POST',
cache: false,
url: validate_livestock/validate_form/field/ + element,
data: element+=+$(selector).val(),
context: document.body,
dataType: 'html',
success: function(data){
if (data == false)
{
$(errorContainer).removeClass('element_valid').addClass('element_error');
invalidElement = element;
alert(invalidElement.join('n'))//for testing purposes
//alert(indexValue);
}
else
{
$(errorContainer).removeClass('element_error').addClass('element_valid');
invalidElement.splice(indexValue, indexValue);
alert(invalidElement.length);//for testing purposes
alert(invalidElement.join('n'))//for testing purposes
}
}
});
}

$(#species).change(function(){
validateElement('#add_livestock', 'species', '#species', '.species_error_1')
});

More From » arrays

 Answers
3

Splice can work in two modes; to remove or insert items.



When removing items you'll specify two parameters: splice(index, length) where index is the starting index, and length is a positive number of elements to remove (fyi: passing a 0, as in your example, does nothing--it's saying remove zero items starting at index). In your case you'll want:



invalidElement.splice(indexValue, 1); // Remove 1 element starting at indexValue


When inserting items you'll specify (at least) three parameters: splice(index, length, newElement, *additionalNewElements*). In this overload you normally pass 0 as a 2nd parameter, meaning to insert the new elements between existing elements.



 var invalidElements = [Invalid2, Invalid3];
invalidElements = invalidElements.splice(0, 0, Invalid1);

[#93485] Tuesday, March 1, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanajamiep

Total Points: 466
Total Questions: 113
Total Answers: 108

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
susanajamiep questions
Sun, Jun 12, 22, 00:00, 2 Years ago
Mon, Mar 7, 22, 00:00, 2 Years ago
Wed, Jun 10, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;