Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Only process if field isn't empty

This is my code: http://jsfiddle.net/E8sNt/1/

I was wondering for the following function:

        if (coded === false) {
                processLocation();
        }

How I can get this to execute ONLY if the #loc input field actually has something in it. In sudo code it would be a bit like this, but I can't work out the proper code:

        if (coded === false && #loc.val!=0) {
                processLocation();
        }

This is my full code:

var coded = false;
geocode();
$.cookie("country", "uk");

// GEOCODE FUNCTION
function geocode() {
        var input = $('#loc')[0];
        var options = {types: ['geocode']};
        var country_code = $.cookie('country');

        if (country_code) {
                options.componentRestrictions = {
                        'country': country_code
                };
        }

        var autocomplete = new google.maps.places.Autocomplete(input, options);

        google.maps.event.addListener(autocomplete, 'place_changed', function() {
                processLocation();
        });

        $('#searchform').on('submit', function(e) {
                if (coded === false) {
                        processLocation();
                }
                return true;
        });

        $("#loc").bind("change paste keyup", function() {
                coded = false;
        });
}

function processLocation() {
        var geocoder = new google.maps.Geocoder();
        var address = $('#loc').val();
        geocoder.geocode({
                'address': address
        },
        function(results, status) {
                if (status === google.maps.GeocoderStatus.OK) {
                        coded = true;
                        $('#lat').val(results[0].geometry.location.lat());
                        $('#lng').val(results[0].geometry.location.lng());
                } else {
                        coded = false;
                        alert("Sorry - We couldn't find this location. Please try an alternative");
                }
        });
//      coded = true;     // Do we need this?
}
like image 622
Jimmy Avatar asked Nov 29 '22 01:11

Jimmy


2 Answers

if (coded === false && $("#loc").val() != "") {
      processLocation();
}

http://jsfiddle.net/E8sNt/2/

Or -

if(!coded && $('#loc').val()){
  processLocation();
}
like image 83
Mohammad Adil Avatar answered Dec 15 '22 09:12

Mohammad Adil


if (coded === false && $('#loc').val() ) {
      processLocation();
}
like image 39
Sully Avatar answered Dec 15 '22 09:12

Sully