I'm building a FAQ plugin for my website in Wordpress but the jQuery doesn't load. I had the same problem when I tried to hide a submit button on a contact form earlier and had to hide it with CSS. Should also say i'm new to jQuery and it's possible I have missed something. Thanks!
PHP:
wp_register_script( 'javascript', plugins_url('js/javascript.js',__FILE__ ));
wp_enqueue_script('javascript');
echo '<div class="qaleft">' . '<div class="toggle">' . '<h3>' . $post_titles . '</h3>' . '<div class="toggle-info">' . '<p>' . $post_content . '</p>' . '</div>' . '</div>' . '</div>';
jQuery:
$(function(){
$('.toggle h3').on('click', function(e){
var answer = $(this).next('.toggle-info');
if(!$(answer).is(":visible")) {
$(this).parent().addClass('open');
} else {
$(this).parent().removeClass('open');
}
$(answer).slideToggle(300);
});
});
In Wordpress jQuery uses no conflict mode, so you cannot use the dollar sign as you are attempting.
To fix use the full name and pass the dollar as a parameter:
jQuery(function($){
//from here on you can use $ as a placeholder to jQuery, as you normally do
$('.toggle h3').on('click', function(e){
var answer = $(this).next('.toggle-info');
if(!$(answer).is(":visible")) {
$(this).parent().addClass('open');
} else {
$(this).parent().removeClass('open');
}
$(answer).slideToggle(300);
});
});
Also, make sure jquery is loaded by including it in the dependancy argument when you register your custom script:
wp_register_script( 'javascript', plugins_url('js/javascript.js',__FILE__ ), array( 'jquery' ));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.