I would like to add some jQuery behavior to a page using the FAPI #ajax behavior (a Commerce product reference add to cart form)
I used a custom module (Commerce Taxonomy Field Product Attribute) to replace the standard text output of the taxonomy term attribute by an image, and hide the radio button of the add to cart form via CSS (display: none). The goal is to let the user click on the image label to select his product's color or size. See a screenshot without css, and with css
I added some custom jquery that adds a "hover" and "selected" class to the label (on mouse over and clicked event) in order to have a ui feedback via CSS - as the Commerce add to cart form uses radios element, there is no "hand" mouse pointer on hover, which can be quite disturbing for the UI. --> It works fine the first time the page is loaded but doesn't get attached when a product is reloaded by ajax.
I have a JS file with
Drupal.behaviors.custom_danity_afficheur_produit_label_hover = {
attach: function (context, settings) {
//All the code that attaches the click / hover event functions using jQuery.once()
}
}
I tried loading the JS file at different level :
- in the template .tpl.php file
- in the add to cart form alter hook in
- a function callback on the add to cart form #post_render attribute
None of these work, the AJAX only works the first time it is loaded.
It's not the first time I try to add jQuery code to an ajax loaded content - I managed last time to modify the code and use FAPI Ajax to make it work - but here i don't know how to do it as the product attributes already have an Ajax behavior implemented by Commerce... and as far is i know, it's not possible to attach multiple ajax behavior to an element with Drupal FormAPI
Any help would be much much much much much appreciated :-)