WordPress - Onclick functionality not working in <a> tags


Michael Stokes

I created a sliding sidebar menu for my WordPress site using only HTML/CSS/JavaScript. This is a tutorial from YouTube and followed everything exactly. The problem is that when I click the button, the error says "Uncaught ReferenceError: openSlideMenu is not defined in HTMLAnchorElement.onclick". For another button I get the same error. See the code below.

HTML (blog.php)

<nav class="navbar">
    <div class="side-menu">
        <span class="open-slide">
            <a href="#" onclick="openSlideMenu()">
               <svg width="30" height="30">
                <path d="M0,5 30,5" stroke="#000" stroke-width="5" />
                <path d="M0,14 30,14" stroke="#000" stroke-width="5" />
                <path d="M0,23 30,23" stroke="#000" stroke-width="5" />
               </svg> 

            </a>
        </span>
    </div>
</nav>

<div id="side-menu" class="side-nav">
    <a  href="#" class="btn-close" onclick="closeSlideMenu()">&times;</a>
        <?php get_sidebar() ?>
</div>



<script type="text/javascript">

    jQuery(document).ready(function(){


    function openSlideMenu() {
        document.getElementById('side-menu').style.width = '250px';
    }

      function closeSlideMenu() {
        document.getElementById('side-menu').style.width = '0';

    }

});

</script>

CSS

.side-nav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top:0;
    left:0;
    background-color: #111;
    opacity: 0.9;
    overflow-x: hidden;
    padding-top: 60px;
    transition: 0.5s;

}

.side-nav a {
    padding: 10px 10px 10px 30px;
    text-decoration: none;
    font-size:22px;
    color: #ccc;
    display: block;
    transition: 0.3s;
}


.side-nav a:hover {
    color: #fff;
}

.side-nav .btn-close {
    position: absolute;
    top: 0;
    right: 22px;
    font-size: 36px;
    margin-left: 50px;
}

The tutorial looks easy to follow and should work. I wonder if this might be related to WordPress? I do my best.

Murshid Ahmed

Since you're using jQuery, why not take full advantage of it?

For more information on what might be the problem, I recommend checking out MarsAndBlack's review

In the meantime, I've made some changes to your code to take full advantage of jQuery.

Here I have added ids for both buttons. (not required, but used for easy detection).

<nav class="navbar">
    <div class="side-menu">
        <span class="open-slide">
            <a href="#" id="openmenu">
               <svg width="30" height="30">
                <path d="M0,5 30,5" stroke="#000" stroke-width="5" />
                <path d="M0,14 30,14" stroke="#000" stroke-width="5" />
                <path d="M0,23 30,23" stroke="#000" stroke-width="5" />
               </svg> 

            </a>
        </span>
    </div>
</nav>

<div id="side-menu" class="side-nav">
    <a  href="#" class="btn-close" id="closemenu">&times;</a>

</div>

Also changed the JS to use jQuery instead of vanilla JS for the click functionality,

jQuery(document).ready(function(){

   jQuery('#openmenu').click(function() {
             jQuery('#side-menu').css('width','250px');

   });

   jQuery('#closemenu').click(function() {
             jQuery('#side-menu').css('width','0px');

   });
});

Here is a working example

Related


navbar div onclick functionality not working on inner elements

JJ Kalk 3 I have a class div in my .dropbtnnavbar and I want the list dropdown to run when clicked, but only the text works when clicked "TOTAL". The <span>and <i>it does nothing inside and when I click on it, I need all three elements to be clickable to show

Button onclick functionality not working properly in jQuery

Sk Aslav I am trying to execute an external function using the button onclick event in asp.net mvc application.Button. But some resign doesn't work and I can't understand the version. <input type="button" id="123" onclick="alert('123')" value="Button 1" /> <i

onclick event not working with option tags in html?

nagging I have a html dropdown menu with a Gobutton , it is working fine for the first two option values where two are selected and then the Gobutton is clicked, but not for the other two option values with onclick event, please help me how it works onclick ev

onclick event not working with option tags in html?

nagging I have a html dropdown menu with a Gobutton , it works fine for two option values that first select two and then Goclick the button, but not for the other two option values that have onclick event, please help How can i work the onclick event <option>i

onclick event not working with option tags in html?

nagging I have a html dropdown menu with a Gobutton , it works fine for two option values that first select two and then Goclick the button, but not for the other two option values that have onclick event, please help How can i work the onclick event <option>i

Why are my conditional tags not working in Wordpress

Ajay Malik In my Wordpress blog BuildinBit . I try Assing H1 to post title instead of blog title on single post and single page. On the home page, archive pages, categories and search pages. I try to assign H1 to my blog title. For this I use the following cod

remove onclick functionality

A. Bunker I'm currently creating new image sliders for a project and I have a problem. The task is to display an image on the page that will open onclick in full size. However, I would like to do this so that the function disappears and the image goes back to

remove onclick functionality

A. Bunker I'm currently creating new image sliders for a project and I have a problem. The task is to display an image on the page that will open onclick in full size. However, I would like to do this so that the function disappears and the image goes back to

remove onclick functionality

A. Bunker I'm currently creating new image sliders for a project and I have a problem. The task is to display an image on the page that will open onclick in full size. However, I would like to do this so that the function disappears and the image goes back to

remove onclick functionality

A. Bunker I'm currently creating new image sliders for a project and I have a problem. The task is to display an image on the page that will open onclick in full size. However, I would like to do this so that the function disappears and the image goes back to

remove onclick functionality

A. Bunker I'm currently creating new image sliders for a project and I have a problem. The task is to display an image on the page that will open onclick in full size. However, I would like to do this so that the function disappears and the image goes back to

Override plugin functionality in WordPress

Michael McGurk I installed the plugin on my WordPress site. I want to override functionality in plugin. Is there a way I can override it in the theme and functions.phpif so how? Here is the original function in my plugin: /** * sensei_start_course_for

Replaces core WordPress functionality

吉迪亚(Jedediah Zilberberg) I'm trying to replace the core WP function wp_delete attachment. In my functions.php file, add the following code: add_action( 'init', 'remove_my_action' ); function remove_my_action(){ remove_action( 'delete_attachment', 'wp_dele

Override plugin functionality in WordPress

Michael McGurk I installed the plugin on my WordPress site. I want to override functionality in plugin. Is there a way I can override it in the theme and functions.phpif so how? Here is the original function in my plugin: /** * sensei_start_course_for

Replaces core WordPress functionality

吉迪亚(Jedediah Zilberberg) I'm trying to replace the core WP function wp_delete attachment. In my functions.php file, add the following code: add_action( 'init', 'remove_my_action' ); function remove_my_action(){ remove_action( 'delete_attachment', 'wp_dele

Override plugin functionality in WordPress

Michael McGurk I installed the plugin on my WordPress site. I want to override functionality in plugin. Is there a way I can override it in the theme and functions.phpif so how? Here is the original function in my plugin: /** * sensei_start_course_for

Override plugin functionality in WordPress

Michael McGurk I installed the plugin on my WordPress site. I want to override functionality in plugin. Is there a way I can override it in the theme and functions.phpif so how? Here is the original function in my plugin: /** * sensei_start_course_for

Replaces core WordPress functionality

吉迪亚(Jedediah Zilberberg) I'm trying to replace the core WP function wp_delete attachment. In my functions.php file, add the following code: add_action( 'init', 'remove_my_action' ); function remove_my_action(){ remove_action( 'delete_attachment', 'wp_dele

Replaces core WordPress functionality

吉迪亚(Jedediah Zilberberg) I'm trying to replace the core WP function wp_delete attachment. In my functions.php file, add the following code: add_action( 'init', 'remove_my_action' ); function remove_my_action(){ remove_action( 'delete_attachment', 'wp_dele

Improve functionality of template tags - Django

Julius Caesar I'm writing a star rating system to graphically calculate the star rating of a course, I use the following template tags: @register.simple_tag def get_course_rating(course): decimal, integer = math.modf(course.get_average_rating()) ratin

Improve functionality of template tags - Django

Julius Caesar I'm writing a star rating system to graphically calculate the star rating of a course, I use the following template tags: @register.simple_tag def get_course_rating(course): decimal, integer = math.modf(course.get_average_rating()) ratin

Improve functionality of template tags - Django

Julius Caesar I'm writing a star rating system to graphically calculate the star rating of a course, I use the following template tags: @register.simple_tag def get_course_rating(course): decimal, integer = math.modf(course.get_average_rating()) ratin

Improve functionality of template tags - Django

Julius Caesar I'm writing a star rating system to graphically calculate the star rating of a course, I use the following template tags: @register.simple_tag def get_course_rating(course): decimal, integer = math.modf(course.get_average_rating()) ratin

Improve functionality of template tags - Django

Julius Caesar I'm writing a star rating system to graphically calculate the star rating of a course, I use the following template tags: @register.simple_tag def get_course_rating(course): decimal, integer = math.modf(course.get_average_rating()) ratin