ashura91 ashura91 - 3 years ago 202
PHP Question

How to activate this JQuery on different event?

I have two combo boxes. One combo box contain values that became variables to do AJAX post. The result then inserted into the second combo box.

I use

onchange
in the first combo box. However, I'd alos like to do it when body
onload


Without rewrite the function, how do I solve my issue?

The combo box jQuery :

$('#tl_dept').on('change', function() {
var dept_val = $(this).val();
var opsi = "";
var divs = "";
$.ajax({
type : "POST",
dataType : "json",
data : { x : dept_val },
context : $(this),
url : "<?php print base_url(); ?>xxx/some_function",
success : function(response){
//success script
}
});
});

Answer Source

I use onchange in the first combo box. However, I'd like to do it when body onload.

You do not need this.

You can use .trigger function which executes all handlers and behaviors attached to the matched elements for the given event type.

$('#tl_dept').on('change', function() {
    var dept_val = $(this).val();
    var opsi = "";
    var divs = "";
    $.ajax({
        type        : "POST",
        dataType    : "json",
        data        : { x : dept_val },
        context     : $(this),
        url         : "<?php print base_url(); ?>xxx/some_function",
        success     : function(response){
            //success script
        }
    });
}).trigger('change');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download