1

I have this function:

jQuery('#sort_order_by').change(function(){ 
        var productUrl = 'product.phtml';
        var value = jQuery(this).val();
        jQuery.cookie('sortorderby', value);            
        var url= productUrl + '?sort_by='+ value;       
        window.location.href = url; 
        jQuery('#sort_order_by').val(jQuery.cookie('sortorderby') || 'helpful' );
    });

When executed, jQuery.cookie('sortorderby') has value jQuery('#sort_order_by').val() not set value of jQuery.cookie('sortorderby').

I think that after the location load it does not re-run the code. So I tried create:

`function jQuery(function {jQuery('#sort_order_by').val(
    jQuery.cookie('sortorderby') || 'helpful' );});`

This gives an error, jQuery.cookie not function.

Can anyone help?

1 Answer 1

7

You need manage dependencies with requirejs:

    require([
        'jquery',
        'jquery/jquery.cookie',
         'domReady!'// wait for dom ready
    ], function($) {
        $('#sort_order_by').change(function() { 
            var productUrl = 'product.phtml';
            var value = $(this).val();
            $.cookie('sortorderby', value);            
            var url= productUrl + '?sort_by='+ value;       
            window.location.href = url; 
            $('#sort_order_by').val($.cookie('sortorderby') || 'helpful' );
        }); 
    }); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.