0

I cant't call a function inside jquery change event

when i use ajax directly inside change event it works fine but when I put ajax in a function and call the function inside change event id doesn't work

 function get_child_cat() {


    var parentID = $('#parent-cat').val();

     $.ajax({
        url: "/Ecom/parsers/prod-child-cat.php",
        method: "POST",
        data: {parentID: parentID},
        success: function(data) {
            $('#child-cat').html(data);
        },
        error: function() {
            alert('something went wrong')
        }
    });

}

$('#parent-cat').on('change', function() {

    get_child_cat();
});

I want to know how to make the function work inside the change event. Thank you

nesma aboki
  • 133
  • 1
  • 10
  • Provided `get_child_cat` is scoped properly, there's nothing wrong with what you have. `get_child_cat` must be scoped at the same level or higher for the change event listener to have access to it. – Taplar May 06 '19 at 21:38
  • Define "doesn't work" (e.g., please see the [How to Ask](https://stackoverflow.com/help/how-to-ask) page). – Dave Newton May 06 '19 at 21:40
  • Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Taplar May 06 '19 at 21:40
  • Thanks for your replies but still can't make it work – nesma aboki May 06 '19 at 22:02

0 Answers0