0

I have a function in my jQuery for my wordpress site. It's in my main.js and above document.ready and my main.js is being loaded after jQuery loads.

Everything works in my static version of the site, it doesn't work when I try to implement it in my theme in wordpress

The function:

function toggleChevron(e) {
    $(e.target)
        .prev('.panel-heading')
        .find("i.indicator")
        .toggleClass('glyphicon-triangle-bottom glyphicon-triangle-left');
}

$('#accordion').on('hidden.bs.collapse', toggleChevron);
$('#accordion').on('shown.bs.collapse', toggleChevron);

It gives this error in my console:

Uncaught TypeError: $ is not a function`

When I try to replace the $ with jQuery I get no errors in my console but it doesn't work. Anyone knows what's going on here? Thanks in advance!

9
  • where is this code on the page? does it execute after jQuery is loaded? Commented Mar 31, 2016 at 14:27
  • If you run the .on() calls before the element with id "accordion" is in the DOM, you'll get no errors but the calls won't do anything. Commented Mar 31, 2016 at 14:27
  • 1
    If the jQuery variable works but the $ does not it means that another plugin has taken the $ reference away from jQuery or that you've used $.noConflict() somewhere. Either way, we can't help you find where in your code that might be. Commented Mar 31, 2016 at 14:27
  • @YarGnawh Yes it's my main.js and my main.js is loaded after jQuery is loaded. I should add that all my other jQuery does work. I edited OP. Commented Mar 31, 2016 at 14:30
  • 1
    @Pointy I am sorry but I don't understand what you mean Commented Mar 31, 2016 at 14:31

1 Answer 1

2

Make sure you're scripts are loading in the correct order, if your website is using jQuery in noconflict mode you have two choices:

(function( $ ) {
  $(function() {
    // Your code here
  });
})(jQuery);

Or

jQuery( document ).ready(function( $ ) {
  // Your code here
});
Sign up to request clarification or add additional context in comments.

1 Comment

It's working for my new feature, but all of the old features aren't working anymore... Even if is in different script or in my html... :/

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.