I am trying to remove a value from an array using splicing. starting at 0 and ending at 0 splicing, but it does not delete the value with index 0. I added the getItemRow function to check the index of the view, which returns 0. I reset the array values ββto the warning, and it still displays the views that should be deleted. invalidElement.splice (indexValue, indexValue); works as expected for indexes that are NOT 0. Why is this happening and how can I remove a value with index 0?
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') });
javascript arrays splice
dottedquad
source share