Reputation: 177
I have the following function that it just won't return true no matter what I do, I don't know, maybe I'm missing something here.
What I'm trying to accomplish is to compare the value of an input field with the options of a datalist. So far, the function compares the values and returns true once one match is found but it just keeps looping a returns false afterward.
function OperatorSelectionCheck() {
if ($("#ModalEmployeeSelect").val() != "")
{
$('#ModalEmployeeSelectList option').filter(function () {
if ($(this).val() === $("#ModalEmployeeSelect").val())
{
$("#ModalEmployeeSelect").removeClass("tberror");
$("#timesheet-modal-errorbag").text("");
return;
}
else
{
$("#ModalEmployeeSelect").focus();
$("#ModalEmployeeSelect").addClass("tberror");
$("#timesheet-modal-errorbag").text("The entry " + $("#ModalEmployeeSelect").val() + " does not match any users in the database." );
return false;
}
});
}
else
{
$("#ModalEmployeeSelect").focus();
$("#ModalEmployeeSelect").addClass("tberror");
$("#timesheet-modal-errorbag").text("Please select an operator.");
return false
}
}
Upvotes: 0
Views: 62
Reputation: 79
Maybe refactoring? I didn't run the code below, but might help you in the right direction.
var collection = $('#ModalEmployeeSelectList option').filter(function () {
return $(this).val() === $("#ModalEmployeeSelect").val()
})
if (collection.length > 0) {
$("#ModalEmployeeSelect").removeClass("tberror");
$("#timesheet-modal-errorbag").text("");
}
else {
$("#ModalEmployeeSelect").focus();
$("#ModalEmployeeSelect").addClass("tberror");
$("#timesheet-modal-errorbag").text("The entry " + $("#ModalEmployeeSelect").val() + " does not match any users in the database." );
}
Upvotes: 1