Vladimir Kyatipov
Vladimir Kyatipov

Reputation: 736

WooCommerce do action on checkout field input

I have this field at woocommerce checkout:

<input type="text" class="input-text wfacp-form-control" name="zipcode" id="zipcode" placeholder="" value="">

I am trying to fire action, when this field is changed. Code i tried is this:

add_action('wp_head', function() {
    if ( is_checkout ) {

        ?><script>
           
     jQuery('input#zipcode').change(function() { 
          alert('Changed!')
     });
    
        </script><?php
    }
});

Unfortunatelly it's not working. Can someone tip me to the right direction? thanks

Upvotes: 2

Views: 760

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

The biggest flaw in your code is that is_checkout should be is_checkout(). Also the way you apply jQuery is wrong

So you get:

// jQuery code
function action_wp_head() {
    if ( is_checkout() ) {
    ?>
    <script type="text/javascript">
    jQuery(function($) {
        // Selector
        $( 'input#zipcode' ).change(function() {
            alert( 'Changed!' );
        });
    });
    </script>
    <?php
    }
}
add_action( 'wp_head', 'action_wp_head' );

Upvotes: 2

Related Questions