Jess McKenzie
Jess McKenzie

Reputation: 8385

jQuery Code not loading

I am debugging some jQuery code and have found that the section of code is not firing when clicked.

My HTML/PHP is:

$cartlink .= "<a class='add_to_cart button' data-id='{$product->id}' href='javascript:;' {$style}>{$label}</a>";

jQuery:

   <script>
        jQuery(window).on('click', 'a.add_to_cart.button', function() {
        console.log("batman");
    }); 
    </script>

Upvotes: 0

Views: 65

Answers (1)

Rahil Wazir
Rahil Wazir

Reputation: 10132

You need to replace the window with document. Because window object doesn't contain DOM Nodes. You however can also do jQuery(window.document) to look for DOM Nodes.

JavaScript Window

The window object represents a window containing a DOM document; the document property points to the DOM document loaded in that window

Upvotes: 2

Related Questions