H
H
habrdima2018-05-17 15:19:04
JavaScript
habrdima, 2018-05-17 15:19:04

I can't see the jquery function in wordpress, what should I do?

My code does not see jquery in wordpress,
I looked on the sites for how to connect,
connected,
develope elements shows in the head a bunch of links to something there and to jquery, so it connected?
my js file inserted after jquery
but in console Uncaught TypeError: $ is not a function
wordpress theme own
js code

$(document).ready(function() {
    alert("fdgsdh")
}
)


functions.php code
<?php
function jquery_init() {
wp_enqueue_script("jquery");
wp_enqueue_script('newscript', get_template_directory_uri() . '/jj.js');
}
if(!is_admin()){
add_action('wp_enqueue_scripts', 'jquery_init');
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2018-05-17
@habrdima

Because WordPress uses jQuery instead of $. You just need to replace, this is the easiest solution

E
EmOtIoN, 2021-04-13
@Apovlitos

There is an even simpler solution, write at the beginning of your script:
var $ = jQuery;
No parentheses, that's important.
You've equated $ to a jQuery function, now when you call $, it will do what you want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question