Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to many if / else statements in JS / JQuery? [closed]

I am trying to compare many page elements from their initial values to what they are changed by the user, to prompt them to save etc.

Is there a better way to do this check without many if / else statements? A switch doesn't work since there are many different values being check, instead of a single one with many cases. Any and all tips / pointers are welcome, thanks!

if ($('#InitialhidLeadType').val() != $('#hidLeadType').val())
        bIsChange = true;
    else if ($('#InitialhidProductType').val() != $('#hidProductType').val())
        bIsChange = true;
    else if ($('#InitialhidFixedFilterType').val() != $('#hidFixedFilterType').val())
        bIsChange = true;
    else if ($('#InitialhidMinCreditScore').val() != $('#hidMinCreditScore').val())
        bIsChange = true;
    else if ($('#InitialhidMaxCreditScore').val() != $('#hidMaxCreditScore').val())
        bIsChange = true;
    else if ($('#InitialhidMinLoanAmount').val() != $('#hidMinLoanAmount').val())
        bIsChange = true;

    else if ($('#InitialhidMinLTV').val() != $('#hidMinLTV').val())
        bIsChange = true;
    else if ($('#InitialhidMaxLTV').val() != $('#hidMaxLTV').val())
        bIsChange = true;
    else if ($('#InitialhidMinCLTV').val() != $('#hidMinCLTV').val())
        bIsChange = true;
    else if ($('#InitialhidMaxCLTV').val() != $('#hidMaxCLTV').val())
        bIsChange = true;
    else if ($('#InitialhidPropertyType').val() != $('#hidPropertyType').val())
        bIsChange = true;
    else if ($('#InitialhidPropertyUse').val() != $('#hidPropertyUse').val())
        bIsChange = true;
    else if ($('#InitialhidBankruptcy').val() != $('#hidBankruptcy').val())
        bIsChange = true;
    else if ($('#InitialhidForeclosure').val() != $('#hidForeclosure').val())
        bIsChange = true;
    else if ($('#InitialhidLoanPurpose').val() != $('#hidLoanPurpose').val())
        bIsChange = true;
    else if ($('#InitialhidIsCashout').val() != $.trim($('#hidIsCashout').val()))
        bIsChange = true;
    else if ($('#InitialhidNoCreditScore').val() != $.trim($('#hidNoCreditScore').val()))
        bIsChange = true;
    else if ($('#InitialhidRelationship').val() != $.trim($('#hidRelationship').val()))
        bIsChange = true;
    else if ($('#InitialhidCurrentLoanVA').val() != $.trim($('#hidCurrentLoanVA').val()))
        bIsChange = true;
    else if ($('#InitialhidFoundHome').val() != $.trim($('#hidFoundHome').val()))
        bIsChange = true;
    else if ($('#InitialhidFHA').val() != $.trim($('#hidFHA').val()))
        bIsChange = true;
    else if ($('#InitialhidIsConforming').val() != $.trim($('#hidIsConforming').val()))
        bIsChange = true;
    else if ($('#InitialhidMSA').val() != $('#hidMSA').val())
        bIsChange = true;
    else if ($('#InitialhidStatedCreditRating').val() != $('#hidStatedCreditRating').val())
        bIsChange = true;
    else if ($('#InitialhidCampEffectivedate').val() != $('#hidCampEffectivedate').val())
        bIsChange = true;
    else if ($('#InitialhidCampExpirationdate').val() != $('#hidCampExpirationdate').val())
        bIsChange = true;
    else if ($('#InitialhidIsFixedFilter').val() != $('#hidIsFixedFilter').val())
        bIsChange = true;
    else if ($('#InitialhidTestCompaign').val() != $('#hidTestCompaign').val())
        bIsChange = true;

    else if ($('#InitialhidSelectedStates').val() != $('#hidSelectedStates').val())
        bIsChange = true;
    else if ($('#InitialhidVolumeTieredPricing').val() != $('#hidVolumeTieredPricing').val())
        bIsChange = true;
    else if ($('#InitialhidWeekDayCapacity').val() != $('#filterCapacity').val())
        bIsChange = true;
    else if ($('#InitialhidWeekendCapacity').val() != $('#filterCapacityWeekEnd').val())
        bIsChange = true;
    else if ($('#InitialhidHolidayCapacity').val() != $('#filterCapacityHoliday').val())
        bIsChange = true;

///////////////////////////////////// HTML Below //////////////////////////////////////

<div id = "changeCheck">

<asp:HiddenField ID="InitialhidLeadType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidProductType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidFixedFilterType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMinCreditScore" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMaxCreditScore" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMinLoanAmount" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMaxLoanAmount" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMinLTV" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMaxLTV" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMinCLTV" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMaxCLTV" ClientIDMode="Static" runat="server" />

<asp:HiddenField ID="InitialhidWeekDayCapacity" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidWeekEndCapacity" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidHolidayCapacity" ClientIDMode="Static" runat="server" />

<asp:HiddenField ID="InitialhidPropertyType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidPropertyUse" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidBankruptcy" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidForeclosure" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidLoanPurpose" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidIsCashout" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidNoCreditScore" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidRelationship" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidCurrentLoanVA" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidFoundHome" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidFHA" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidIsConforming" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidMSA" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidStatedCreditRating" ClientIDMode="Static" runat="server" />

<asp:HiddenField ID="InitialhidCampEffectivedate" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidCampExpirationdate" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidIsFixedFilter" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidTestCompaign" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidSelectedStates" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="InitialhidVolumeTieredPricing" ClientIDMode="Static" runat="server" />

like image 408
arcade16 Avatar asked Jan 12 '23 21:01

arcade16


1 Answers

Iterate the elements, compare each one, and if there are changes, prompt the user to save:

var save = false;

$('[id^="Initial"]').each(function() {
    if ( this.value != $('#'+this.id.replace('Initial','')).val()) save = true;
});

if (save) confirm(' would you like to save ');

Normally you'd solve this a different way, by changing a variable when the element is changed, and not with a check of all elements at some time:

var save = false;

$(':input').on('change', function() {
    save = true;
});

window.onbeforeunload = function() {
    if (save) confirm(' would you like to save ');
}
like image 200
adeneo Avatar answered Jan 16 '23 00:01

adeneo