Remie
Remie

Reputation: 75

Adjusting jQuery to work on Wordpress (syntaxerror)

I have a line of code that has a "$" value in it, which Wordpress doesn't seem to accept. How to adjust the "$", so that Wordpress will read it correctly?

jQuery(document).bind('gform_post_render',function(){
    jQuery('#input_24_4').change(function(){
        jQuery('#input_24_3').data('amount',$(this).val());
    });
});

Upvotes: 0

Views: 34

Answers (2)

Eduardo Escobar
Eduardo Escobar

Reputation: 3389

$ should be an alias of jQuery, anyway, for some reason $ is not defined sometimes. You can fix it by using an anonymous function:

(function($) {
    $(document).bind('gform_post_render',function() {
        $('#input_24_4').change(function(){
            $('#input_24_3').data('amount',$(this).val());
        });
    });
})(jQuery);

Also, make sure you've loaded jQuery library before embedding / executing this piece of code.

Upvotes: 2

Boldewyn
Boldewyn

Reputation: 82734

Where is this line of code? If it's part of a double-quoted string in any of your PHP files, you need to escape the $ with a backslash: \$

Apart from that, jQuery runs in noConflict mode in Wordpress. That means, it doesn't set the global $ variable, just the jQuery name.

If you want to change that, you need to set it yourself somewhere before this line:

window.$ = window.jQuery;

Upvotes: 0

Related Questions