2

Followingjavascript has getAjax function. which was call every mouse select event in the html form. I have to call same function in each event. When i want to modify getAjax function, have to do for all event. Is there any way to keep getAjax function one place and call in each mouse event.

Note : all select elements are in the same container

<script type = "text/javascript" >
  $(document).ready(function() {
    var age1 = 21;
    var age2 = 35;
    var salary = 10000;
    var width = 410;
    var height = 510;
    getAjax(ajaxUrl, salary, age1, age2, width, height);
    $("#age1").change(function() {
      age1 = parseInt($(this).val());
      getAjax(ajaxUrl, salary, age1, age2, width, height);
    });
    $("#age2").change(function() {
      age2 = parseInt($(this).val());
      getAjax(ajaxUrl, salary, age1, age2, width, height);
    });
    $("#salary").change(function() {
      salary = parseInt($(this).val());
      getAjax(ajaxUrl, salary, age1, age2, width, height);
    });
    $("#width").change(function() {
      width = parseInt($(this).val());
      getAjax(ajaxUrl, salary, age1, age2, width, height);
    });
    $("#height").change(function() {
      height = parseInt($(this).val());
      getAjax(ajaxUrl, salary, age1, age2, width, height);
    });
  });

function getAjax(URL, salary, age1, age2, width, height) {
  $.ajax({
    url: URL,
    data: {
      sala: salary,
      age1: age1,
      age2: age2,
      het1: width,
      het2: height
    },
    dataType: "html",
    type: "POST",
    success: function(retdata) {
      $("#data1").html(retdata);
    }
  });
} </script>

3
  • If all the select elements are in the same container, I'd just use one event handler on that parent container and let the change events bubble up to it. If you do need to have a change handler on each select seperately, create them in a loop, since the change functions are all the same apart from the variable to update. Commented Aug 11, 2016 at 14:19
  • you can put onchange function in the element like <input type="text" onchange="getAjax(URL, salary, age1, age2, width, height)"> and pass the values accordingly. Commented Aug 11, 2016 at 14:20
  • @Shilly yes all select elements are in the same container. Commented Aug 11, 2016 at 14:24

2 Answers 2

6

You can combine the selector and you could rewrite the variables, maybe into an object, which make everything more compact.

$(function() {
    var data = {
        age1   : 21,
        age2   : 35,
        salary : 10000,
        width  : 410,
        height : 510
    };

    getAjax(ajaxUrl, data.salary, data.age1, data.age2, data.width, data.height);

    $("#age1, #age2, #salary, #width, #height").change(function() {
        data[$(this).attr("id")] = parseInt($(this).val());
        getAjax(ajaxUrl, data.salary, data.age1, data.age2, data.width, data.height);
    });
});

Even better would be if you change your getAjax function to work with the object too. This will save you even more code.

$(function() {
    var data = {
        age1   : 21,
        age2   : 35,
        salary : 10000,
        width  : 410,
        height : 510
    };

    getAjax(ajaxUrl, data);

    $("#age1, #age2, #salary, #width, #height").change(function() {
        data[$(this).attr("id")] = parseInt($(this).val());
        getAjax(ajaxUrl, data);
    });
});

function getAjax(URL, data) {
    $.ajax({
        url: URL,
        data: data,
        dataType: "html",
        type: "POST",
        success: function(retdata) {
            $("#data1").html(retdata);
        }
    });
} 
Sign up to request clarification or add additional context in comments.

7 Comments

Alternatively you could also place the same class on all the html elements to condense the jQuery even more to something like $(".person").change(function() { ... });
@eisbehr according to my JS i can access salary in php page as $salary = $this->input->post('sala'); . You had pass not variable but entire array. can you please explain how to access individual values in php code which you had pass as array data.
Exacty the same way. Just change 'sala' to 'salary'. And the same vor het1 and het2. Thats all. @GeethWelagedara
@eisbehr there were small code error in data[$(this).attr("id")] = parseInt($(this).val()); when select options it doesn't appear values. But without array working (as @peternyc's answer following). I think there is a mistake in data array value assigning when select is selected.
And? Any problems there? Selects work fine too. jsfiddle.net/2pr8fb10/9 @GeethWelagedara
|
1

Kind of inefficient, which won't matter, but you only have one event listener in the code.

<script type = "text/javascript" >
  $(document).ready(function() {
    var age1 = 21;
    var age2 = 35;
    var salary = 10000;
    var width = 410;
    var height = 510;
    getAjax(ajaxUrl, salary, age1, age2, width, height);
    $("#age1,#age2,#salary,#width,#height").change(function() {
      age1 = parseInt($('#age1').val());
      age2 = parseInt($('#age2').val());
      salary = parseInt($('#salary').val());
      width = parseInt($('#width').val());
      height = parseInt($('#height').val());
      getAjax(ajaxUrl, salary, age1, age2, width, height);
    });
  });

function getAjax(URL, salary, age1, age2, width, height) {
  $.ajax({
    url: URL,
    data: {
      sala: salary,
      age1: age1,
      age2: age2,
      het1: width,
      het2: height
    },
    dataType: "html",
    type: "POST",
    success: function(retdata) {
      $("#data1").html(retdata);
    }
  });
} </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.