From a1f02e706f0be8807dad8aae6ef18e2f1d9e22d3 Mon Sep 17 00:00:00 2001 From: Daniela Feitosa Date: Mon, 7 Apr 2014 22:55:26 +0000 Subject: [PATCH] Autocomplete fields on page load --- public/javascripts/city_state_validation.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/public/javascripts/city_state_validation.js b/public/javascripts/city_state_validation.js index 29c96b0..37c7bed 100644 --- a/public/javascripts/city_state_validation.js +++ b/public/javascripts/city_state_validation.js @@ -1,55 +1,60 @@ (function($){ + autoCompleteStateCity($); $('[id$="_country"]').change(function(){ - var country_selected = $('[id$="_country"] option:selected').val() - if(country_selected == "BR") - { - $('#state_field').autocomplete({ - source : function(request, response){ - $.ajax({ - type: "GET", - url: '/account/search_state', - data: {state_name: request.term}, - success: function(result){ - response(result); - }, - error: function(ajax, stat, errorThrown) { - console.log('Link not found : ' + errorThrown); - } - }); - }, + autoCompleteStateCity($); + }) +})(jQuery); + +function autoCompleteStateCity($) { + var country_selected = $('[id$="_country"] option:selected').val() + if(country_selected == "BR") + { + $('#state_field').autocomplete({ + source : function(request, response){ + $.ajax({ + type: "GET", + url: '/account/search_state', + data: {state_name: request.term}, + success: function(result){ + response(result); + }, + error: function(ajax, stat, errorThrown) { + console.log('Link not found : ' + errorThrown); + } + }); + }, - minLength: 3 - }); + minLength: 3 + }); - $('#city_field').autocomplete({ - source : function(request, response){ - $.ajax({ - type: "GET", - url: '/account/search_cities', - data: {city_name: request.term, state_name: $("#state_field").val()}, - success: function(result){ - response(result); - }, - error: function(ajax, stat, errorThrown) { - console.log('Link not found : ' + errorThrown); - } - }); - }, + $('#city_field').autocomplete({ + source : function(request, response){ + $.ajax({ + type: "GET", + url: '/account/search_cities', + data: {city_name: request.term, state_name: $("#state_field").val()}, + success: function(result){ + response(result); + }, + error: function(ajax, stat, errorThrown) { + console.log('Link not found : ' + errorThrown); + } + }); + }, - minLength: 3 - }); + minLength: 3 + }); + } + else + { + if ($('#state_field').data('autocomplete')) { + $('#state_field').autocomplete("destroy"); + $('#state_field').removeData('autocomplete'); } - else - { - if ($('#state_field').data('autocomplete')) { - $('#state_field').autocomplete("destroy"); - $('#state_field').removeData('autocomplete'); - } - if ($('#city_field').data('autocomplete')) { - $('#city_field').autocomplete("destroy"); - $('#city_field').removeData('autocomplete'); - } + if ($('#city_field').data('autocomplete')) { + $('#city_field').autocomplete("destroy"); + $('#city_field').removeData('autocomplete'); } - }) -})(jQuery); + } +} -- libgit2 0.21.2