Created
April 14, 2021 05:45
-
-
Save DeanPoulin/b95dc16e5c3df1c807521f6ec5fe1b33 to your computer and use it in GitHub Desktop.
jQuery That Needs Some Help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here's some jQuery/javascript that could use lots of help. This is NOT my code. | |
// This would be some great code to run through quick-lint-js | |
function hide_error(e,id){ | |
e.preventDefault(); | |
jQuery("input").attr("autocomplete","new-address"); | |
jQuery('#'+id+'_error').addClass('hide_err').hide(); | |
if(id=="enter_address"){ | |
jQuery("#selectUnit").val(0); | |
} | |
} | |
function form_property_search(e){ | |
e.preventDefault(); | |
var add_addr = jQuery.trim(jQuery("#enter_address").val()); | |
var add_city = jQuery.trim(jQuery("#city").val()); | |
var add_state = jQuery.trim(jQuery("#state").val()); | |
var add_zip = jQuery.trim(jQuery("#zip_code").val()); | |
var add_unit = jQuery.trim(jQuery("#selectUnit").val()); | |
var addr_err_text = "Can't be empty!"; | |
var is_advance_search = jQuery('#is_advance_search').val(); | |
var is_error=0; | |
var add_search_method = 'API Auto Complete'; | |
if(is_advance_search=="1"){ | |
addr_err_text = "The Address field is required"; | |
add_search_method = "Advanced Search"; | |
} | |
if (add_addr=="") { | |
jQuery('#enter_address_error').removeClass('hide_err').text(addr_err_text).show(); | |
is_error=1; | |
} | |
if(is_advance_search=="1"){ | |
if (add_city=="") { | |
jQuery('#city_error').removeClass('hide_err').text("The City field is required").show(); | |
is_error=1; | |
} | |
if (add_state=="") { | |
jQuery('#state_error').removeClass('hide_err').text("The State field is required").show(); | |
is_error=1; | |
} | |
// if (add_zip=="") { | |
// jQuery('#zip_code_error').removeClass('hide_err').text(err_text).show(); | |
// is_error=1; | |
// } | |
} | |
if(is_error){ | |
return false; | |
} | |
var base_url = "https://app.example.com/signup"; | |
var query_string = ""; | |
if(is_advance_search=="0"){ | |
var d = jQuery("#enter_address").val(); | |
var address = d.split(','); | |
add_addr = jQuery.trim(address[0]); | |
add_city = jQuery.trim(address[1]); | |
add_state = jQuery.trim(address[2]); | |
add_zip = jQuery.trim(address[3]); | |
} | |
if(add_unit !="0" && add_unit!=""){ | |
if(add_unit=="Condominium Association/Building Owner"){ | |
add_addr = add_addr+' Condo'; | |
}else{ | |
add_addr = add_addr+', '+add_unit; | |
} | |
} | |
query_string = `?address=${encodeURIComponent(add_addr)}&city=${encodeURIComponent(add_city)}&state=${encodeURIComponent(add_state)}&zip=${encodeURIComponent(add_zip)}&search_method=${encodeURIComponent(add_search_method)}`; | |
var final_url = base_url+query_string; | |
location.href = final_url; | |
} | |
function search_by_add(e){ | |
e.preventDefault(); | |
jQuery('#manual_search').hide(); | |
jQuery('#wpsearch12').removeClass('ad-search'); | |
jQuery('#ad_search').show(); | |
jQuery('#search_by_add').hide(); | |
var add_addr = jQuery("#enter_address").val(); | |
var add_city = jQuery("#city").val(); | |
var add_state = jQuery("#state").val(); | |
var add_zip = jQuery("#zip_code").val(); | |
var full_add = ""; | |
if(add_addr) | |
full_add = add_addr; | |
if(add_city) | |
full_add += ", "+add_city; | |
if(add_state) | |
full_add += ", "+add_state; | |
if(add_zip) | |
full_add += ", "+add_zip; | |
if(full_add) | |
jQuery("#enter_address").val(full_add); | |
jQuery('#is_advance_search').val(0); | |
} | |
function cant_find_add(e){ | |
e.preventDefault(); | |
jQuery('#manual_search').show(); | |
jQuery('#search_by_add').show(); | |
jQuery('#wpsearch12').addClass('ad-search'); | |
jQuery('#ad_search').hide(); | |
var d = jQuery("#enter_address").val(); | |
var address = d.split(','); | |
jQuery("#enter_address").val(jQuery.trim(address[0])); | |
jQuery("#city").val(jQuery.trim(address[1])); | |
jQuery("#state").val(jQuery.trim(address[2])); | |
jQuery("#zip_code").val(jQuery.trim(address[3])); | |
jQuery('#is_advance_search').val(1); | |
jQuery('#enter_address_error').hide().addClass('hide_err'); | |
jQuery('#city_error').hide().addClass('hide_err'); | |
jQuery('#state_error').hide().addClass('hide_err'); | |
//jQuery('#zip_code_error').hide(); | |
} | |
var timer = null; | |
function callapi(e) { | |
e.preventDefault(); | |
var is_advance_search = jQuery('#is_advance_search').val(); | |
if (is_advance_search=="0") { | |
clearTimeout(timer); | |
timer = setTimeout(function(){ | |
var loadmOption = []; | |
loadmOption = loadApiOption(); | |
$('#enter_address').autocomplete({ | |
lookup: loadmOption, | |
onSelect: function (suggestion) { | |
if(suggestion.suitlist.length){ | |
jQuery('#selectUnit').find('option').remove().end(); | |
jQuery('#unitdiv').show(); | |
jQuery('#wpsearch12').addClass('unit-visible'); | |
jQuery.each(suggestion.suitlist, function (i, item) { | |
jQuery('#selectUnit').append(jQuery('<option>', { | |
value: item, | |
text : item | |
})); | |
}); | |
}else{ | |
jQuery('#unitdiv').hide(); | |
jQuery('#wpsearch12').removeClass('unit-visible'); | |
} | |
} | |
}); | |
$( "#enter_address" ).focus(); | |
},800); | |
} | |
} | |
function loadApiOption(){ | |
var arrayReturn = []; | |
var arraywithsuitlist = []; | |
var arraywithoutsuitlist = []; | |
var d = jQuery("#enter_address").val(); | |
jQuery.ajax({ | |
url: "https://api.example.com/address/typeahead?address="+d, | |
async: false, | |
dataType: 'json', | |
success: function (data) { | |
for (var i = 0, len = data.Results.length; i < len; i++) { | |
var id = (data.Results[i].full_address).toString(); | |
var suitlist = data.Results[i].Address.SuiteList; | |
if (suitlist.length){ | |
arraywithsuitlist.push({'value' : data.Results[i].full_address, 'data' : id,'suitlist':suitlist}); | |
}else{ | |
arraywithoutsuitlist.push({'value' : data.Results[i].full_address, 'data' : id,'suitlist':suitlist}); | |
} | |
} | |
} | |
}); | |
var filterdata = []; | |
if(arraywithsuitlist.length){ | |
jQuery.each(arraywithsuitlist, function( index, value ) { | |
filterdata = jQuery.grep(arraywithoutsuitlist, function(e){ | |
if( e.data == value.data){ | |
value.suitlist.unshift('Condominium Association/Building Owner') | |
} | |
return e.data != value.data; | |
}); | |
}); | |
arrayReturn = jQuery.merge(arraywithsuitlist, filterdata); | |
}else{ | |
arrayReturn = jQuery.merge(arraywithoutsuitlist, filterdata); | |
} | |
return arrayReturn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment