In WP jQuery works in compatibility mode by default. It means that the typical $ shortcut for jQuery doesn’t work, so it doesn’t conflict with any other JavaScript libraries that use the dollar sign also, like MooTools or Prototype.
So you should use jQuery
instead of $
in your JS file.
Of course you can wrap the code in an anonymous function where you pass in jQuery
to be mapped to $
.
(function($) {
// $ Works here!
// console.log($);
})( jQuery );
You also have to add jquery as a dependency when you enqueue the script:
function myprefix_enqueue_scripts() {
wp_enqueue_script( 'my-script', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'), true );
}
add_action( 'wp_enqueue_scripts', 'myprefix_enqueue_scripts' );
PS. I assume that your file is enqueued properly and it really is in your code.
Related Posts:
- How do I check if an array includes a value in JavaScript?
- JavaScript document.getElementById().value logs empty value
- What is console.log?
- Can I use complex HTML with Twitter Bootstrap’s Tooltip?
- How do I redirect to another webpage?
- Invariant Violation: Objects are not valid as a React child
- Compare two dates with JavaScript
- How can I merge properties of two JavaScript objects dynamically?
- In reactJS, how to copy text to clipboard?
- SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag
- Uncaught SyntaxError: Invalid or unexpected token
- JavaScript TypeError: Cannot read property ‘style’ of null
- Invalid shorthand property initializer
- nodejs – error self signed certificate in certificate chain
- How do I loop through or enumerate a JavaScript object?
- Getting Unexpected Token Export
- Adding images to a Bootstrap Carousel with AJAX
- XMLHttpRequest cannot load XXX No ‘Access-Control-Allow-Origin’ header
- Uncaught TypeError: Cannot read property ‘props’ of null
- How to use iMonkey in an iOS app
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- Mouseover & Mouseout w/ Javascript
- Uncaught TypeError: Cannot read property ‘msie’ of undefined – jQuery tools
- Angular JS Uncaught Error: [$injector:modulerr]
- Nested objects in javascript, best practices
- Scroll Automatically to the Bottom of the Page
- What does window.jQuery and window.$ mean?
- How to change date format in JavaScript
- OnChange event handler for radio button (INPUT type=”radio”) doesn’t work as one value
- InvalidValueError: not an instance of HTMLInputElement
- How to clear react-native cache?
- Execute PHP function with onclick
- blocked a frame of origin “null” from accessing a cross-origin frame – chrome
- How can I resolve the error “URL scheme must be “http” or “https” for CORS request.” for this code [duplicate]
- Initializing an Array with a Single Value
- Is it possible to stop JavaScript execution?
- Remove commas from the string using JavaScript
- How can you remove all documents from a collection with Mongoose?
- How to set Angular 4 background image?
- React-Router : What is the purpose of IndexRoute?
- SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
- Add new attribute (element) to JSON object using JavaScript
- D3 Appending Text to a SVG Rectangle
- JQuery get the title of a button
- How to open a URL using PHP?
- Trying to load jquery into tampermonkey script
- Atom JavaScript Autocomplete
- How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?
- JavaScript button onclick not working
- Invariant Violation: _registerComponent(…): Target container is not a DOM element
- How to count string occurrence in string?
- Angular 2 Hover event
- getElementsByClassName not working [duplicate]
- Javascript: Unicode string to hex
- Preloading images with jQuery
- How to change cursor from pointer to finger using jQuery?
- How to get value in an object’s key using a variable referencing that key?
- Button that refreshes the page on click
- jQuery equivalent of body onLoad
- Replace spaces with dashes and make all letters lower-case
- How to make `setInterval` behave more in sync, or how to use `setTimeout` instead?
- Adding Google Translate to a web site
- Javascript: getFullyear() is not a function
- Must use destructuring props assignment
- Line continuation characters in JavaScript
- Simulate a click on ‘a’ element using javascript/jquery
- How can I remove a style added with .css() function?
- Trigger Javascript on Gutenberg (Block Editor) Save
- Show control conditionally in Gutenberg
- How do I include js without register/enqueue?
- Admin Bar menu item that executes Javascript but does not reload the page?
- wp_customize_support_script – do I need it?
- Customizer: When to use customize_preview_init VS customize_controls_enqueue_scripts
- Post to WordPress using REST API from external site
- Set current tab on a Gutenberg TabPanel component from outside that component
- Does wp_localize_script call a Javascript Function or Simply Pass PHP Parameters to It
- How to remove p / br elements from gutenbergs editor
- 404 Error Contact Form 7 on WordPress
- contact form 7 repeatable fields calculations [closed]
- Listening change event of taxonomy term checkboxes in Gutenberg editor
- Accessing theme customizer farbtastic object instances
- Add custom classes for blocks in editor based on custom attributes
- How to locate a Javascript? It is there in HTML but not in any file [closed]
- Trigger wp-embed via JavaScript to refresh iframe preview?
- Malware gdjfgjfgj235f [closed]
- Including Styles and JS files to work ON my plugin interface
- Gutenberg – useEffect manipulate DOM after block re-renders DOM
- how to add a custom javascript file to our theme
- Why does my array sort order changes when I pass it to JS using WP_REST_Response?
- WordPress Guternberg Content For React Native
- Custom block update rendering when reused
- Spans in gutenberg
- Get a default customizeAction text for a section using Customizer JS API
- contact form 7 javascript and wpcf7_before_send_mail action
- custom page url slug needs illegal ?id=1 for javascript
- Use one javascript variable into another javascript file
- Embed Kajabi into WP
- Passing object to FormTokenField suggestions
- How to Update a variable even if the web page reloads in js
- How do i get an Inline style in Gutenberg Block show up in front end?