Magento 1.9 – How to Change onClick to onLoad for Displaying Products Automatically

javascriptmagento-1.9

I'm using the b2b order list extension which changes the way products are displayed (in this example I'm using grouped products)…

By default you have to click a link for the simple products to become visible, the links is…

<a href="#" id="openproducts" onclick="getProductOptions(this); return false;">

that triggers the following function;

    function getProductOptions(link){
        jQuery(link).parents("tr").next("tr").find('.complex-product-children').find("select").each(function(){
            if(jQuery(this).hasClass('required-entry-b2borderlist')){
                jQuery(this).removeClass('required-entry-b2borderlist').addClass('required-entry');
            }
        })

        jQuery(link).parents("tr").next("tr").find('.complex-product-children').find("input[type='radio']").each(function(){
            if(jQuery(this).hasClass('validate-one-required-by-name-b2borderlist')){
                jQuery(this).removeClass('validate-one-required-by-name-b2borderlist').addClass('validate-one-required-by-name');
            }
        })

        jQuery(link).parents("tr").next("tr").find('.complex-product-children').find("input[type='checkbox']").each(function(){
            if(jQuery(this).hasClass('validate-one-required-by-name-b2borderlist')){
                jQuery(this).removeClass('validate-one-required-by-name-b2borderlist').addClass('validate-one-required-by-name');
            }
        })
        jQuery(link).parents("tr").next("tr").find('.complex-product-children').show();
    }

Is there any way for me to make it so the link doesn't need to be clicked and its automatically open? I tried doing a trigger click like this;

jQuery(document).ready(function(){
 jQuery("#openproducts").trigger('click');
});

Which works but only for the first product?

Thanks in advance

Best Answer

You cannot do it using id of the element. You need to add a class to your a tag and then loop through all links to trigger click event.

<a href="#" class="openproducts" id="openproducts" onclick="getProductOptions(this); return false;">

Trigger click on each element.

jQuery(document).ready(function(){
   jQuery(".openproducts").each(function (){
       jQuery(this).trigger('click');
   });
});
Related Topic