I'm having a major problem trying to do something that is very simple. I'm pushing elements into an array based on the value of a select element.
I'm pushing elements in fine, but if the user changes the select option to 'No' i want to remove a particular element from the array. I have google far and wide and tried lots of things and the array splice method seems to be the way to go.
My issue is, that according to the documentation, the returned array is the elements that are removed. I don't want this, i want the elements that are remaining in the array.
The code i have implemented almost works but not quite. I can add into the array and if there is only one array item i can remove it OK. If there is more than one item inside, then when i remove the required element it returns the removed element inside the array, where as i want to return the remaining elements inside the array.
Here is my code so far
var pplArray = [];
$("#ddl_KnowlComp").change(function () {
if($('#ddl_KnowlComp').find("option:selected").text() == 'Yes' ){
$('#d_KnowlCompDate').attr('required', 'required');
pplArray.push( $("#ppl_KnowlSigned_TopSpan_HiddenInput"));
console.log(pplArray);
} else {
$('#d_KnowlCompDate').removeAttr('required');
$('#d_KnowlCompDate').val('');
var itemtoRemove = $("#ppl_KnowlSigned_TopSpan_HiddenInput");
removeArrayItem(itemtoRemove,pplArray);
}
});
$("#ddl_IniComEngComp").change(function () {
if($('#ddl_IniComEngComp').find("option:selected").text() == 'Yes' ){
$('#d_IniComEngCompDate').attr('required', 'required');
pplArray.push ($("#ppl_IniComEngCompSigned_TopSpan_HiddenInput"));
console.log(pplArray);
} else {
$('#d_IniComEngCompDate').removeAttr('required');
$('#d_IniComEngCompDate').val('');
var itemtoRemove = $("#ppl_IniComEngCompSigned_TopSpan_HiddenInput");
removeArrayItem(itemtoRemove,pplArray);
}
});
function removeArrayItem(itemtoRemove, arr){
arr.splice($.inArray(itemtoRemove, arr),1);
console.log(pplArray);
}
If someone could point out my issue that would be great :-)