Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I do need to use jquery autocomplete for two inputs. But I have use same processing script for these two elements.

At this stage I am doing it like this.

    $("#suburb").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: "includes/state_au_ajax.php",
            dataType: "json",
            method: "post",
            data: {
                term : request.term,
                state : $("#state").val()
            },
                        success: function( data ) {
                             response( $.map( data, function( item ) {
                                var code = item.split("|");
                                return {
                                    label: code[0],
                                    value: code[0],
                                    data : item
                                }
                            }));
                        }
        });
    },
    autoFocus: true,            
    minLength: 2,
      select: function( event, ui ) {
            var output = ui.item.data.split("|");                       
            $('#zip_code').val(output[1]);
        },  
    delay: 300
  });   

    // --- Populate ZIP code according to the value of "Suburb"
    $("#p_suburb").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: "includes/state_au_ajax.php",
            dataType: "json",
            method: "post",
            data: {
                term : request.term,
                state : $("#p_state").val()
            },
                        success: function( data ) {
                             response( $.map( data, function( item ) {
                                var code = item.split("|");
                                return {
                                    label: code[0],
                                    value: code[0],
                                    data : item
                                }
                            }));
                        }
        });
    },
    autoFocus: true,            
    minLength: 2,
      select: function( event, ui ) {
            var output = ui.item.data.split("|");                       
            $('#p_zip_code').val(output[1]);
        },  
    delay: 300
  });   

My question is, Can I write this without any code duplication?

share|improve this question

migrated from stackoverflow.com Nov 23 '15 at 20:50

This question came from our site for professional and enthusiast programmers.

up vote 0 down vote accepted

You can create separate function like this:

function autoComplete($obj, $valueObj, $zipValue){
        $obj.autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "includes/state_au_ajax.php",
                    dataType: "json",
                    method: "post",
                    data: {
                        term : request.term,
                        state : $valueObj.val()
                    },
                                success: function( data ) {
                                     response( $.map( data, function( item ) {
                                        var code = item.split("|");
                                        return {
                                            label: code[0],
                                            value: code[0],
                                            data : item
                                        }
                                    }));
                                }
                });
            },
            autoFocus: true,            
            minLength: 2,
              select: function( event, ui ) {
                    var output = ui.item.data.split("|");                       
                    $zipValue.val(output[1]);
                },  
            delay: 300
          });  
    }

And then call it appropriately:

    autoComplete($("#suburb"), $("#state"), $('#zip_code'))
    autoComplete($("#p_suburb"), $("#p_state"), $('#p_zip_code'))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.