Do something if product type is changed

71
December 01, 2020, at 9:20 PM

I have a custom WooCommerce product type. If you switched between the standard WooCommerce type the input are cleared but this does not work for custom types.

So I created a jQuery for this

$('#product-type').on('change', function() {
  //do something
});

But this is not the solution because the functions in this code are also running if the page is refreshed.

What can I do to let this work only if the product type is changing

Answer 1

WooCommerce set the product type with JavaScript and is not set wit PHP. They use the change function if the page is loading and set the right product type on screen.

If you need a function on a specific product type you can do something like this

$('#product-type').on('change', function() {
    if($('#product-type').val() == '//your type'){
        //do something
    }
});
Answer 2

Put your code inside the ready event. The code inside this function fires only when the DOM is completely loaded.

$(document).ready(function(){
    $('#product-type').on('change', function() {
        //do something
    });
});
READ ALSO
Why IntelliJ has inspections for generic usage disabled by default?

Why IntelliJ has inspections for generic usage disabled by default?

Why does IntelliJ has inspections for generic usage disabled by default?

72
Dynamically check value and display alarm popover

Dynamically check value and display alarm popover

I want to validate user input so he only provides numbers from a certain range with 0,5 stepsBut I want my website to do it every time user swaps to another input form, not after he sends the data to my view

36
PostgreSQL Database not connecting to Android app

PostgreSQL Database not connecting to Android app

My database is running fine because I tested it using Eclipse with simple Java code that just connects and reports back success or failI connect fine

38