Chris Cullen
Chris Cullen

Reputation: 110

WordPress Edit Page editor unresponsive - jQuery Error?

When I go to edit my page I cannot open screen options or switch between visual and Text mode in the editor.

I've tried adding

define('CONCATENATE_SCRIPTS', false);
define('SCRIPT_DEBUG', true);

to wp config but it hasn't helped

My jQuery load script is this

function loadJquery()
{
    if ($hook != 'post-new.php' || $hook != 'post.php' ) {
        wp_register_script('siteJs',get_template_directory_uri() . 
'/js/site.js',array('jquery'),'1.4.1');
        wp_enqueue_script('siteJs'); // Enqueue it!
    }
}

I get this error:

Uncaught TypeError: Cannot read property 'addEventListener' of null

Referring to this:

(function() {
    var burger = document.querySelector('.navbar-burger');
    var menu = document.querySelector('.navbar-menu');
    burger.addEventListener('click', function() {
    burger.classList.toggle('is-active');
    menu.classList.toggle('is-active');
});

And as the page sits there it also brings up this error:

Uncaught TypeError: Cannot read property 'hasClass' of undefined
at HTMLDocument. (wp-auth-check.js:101)

Upvotes: 0

Views: 866

Answers (2)

Chris Cullen
Chris Cullen

Reputation: 110

I was using

wp_print_scripts

to load jQuery, when as dafoxuk said I should be using

wp_enqueue_scripts()

Upvotes: 0

dafoxuk
dafoxuk

Reputation: 469

The error Uncaught TypeError: Cannot read property 'addEventListener' of null is indicating that burger is undefined. While it may be worth reviewing whether site.js should be running on the WP Admin (where .navbar-burger doesn't even exist), the direct fix is to check whether burger is found before adding the event listener.

This would look like:

(function() {
var burger = document.querySelector('.navbar-burger');
var menu = document.querySelector('.navbar-menu');
if (burger != null && menu !=  null){
     burger.addEventListener('click', function() {
     burger.classList.toggle('is-active');
     menu.classList.toggle('is-active');
  });
}
})();

Upvotes: 1

Related Questions