javascript - Alternative to many if / else statements in JS / JQuery? -


i trying compare many page elements initial values changed user, prompt them save etc.

is there better way check without many if / else statements? switch doesn't work since there many different values being check, instead of single 1 many cases. , tips / pointers 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" /> 

iterate elements, compare each one, , if there changes, prompt user save:

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

normally you'd solve different way, changing variable when element changed, , not check of elements @ time:

var save = false;  $(':input').on('change', function() {     save = true; });  window.onbeforeunload = function() {     if (save) confirm(' save '); } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -