Incorporating Keyboard Features into Buttons

How can I toggle the page selectors in #pageList using a keyboard shortcut instead of clicking on the .togglePL button? I've tried looking up solutions online and asking questions here, but haven't found a working solution yet. Below is the code snippet for reference. Any help would be greatly appreciated. Thank you!

// Toggle Page List
$(document).ready(function() {
    $('.togglePL').click(function(e) {
        if ($('#pageList').width() == 40) {
            $('.textPL').show();
            $('#pageList').width(168);
            $('.togglePL').css({left:'192px', transform: 'none'});
             $('#pageList a').css({fontSize:'10pt'});
        }
        else {
            $('.textPL').hide();
            $('#pageList').width(40);
            $('.togglePL').css({left:'64px', transform: 'rotate(180deg)', transitionDuration:'0.3s'});
            $('#pageList a').css({fontSize:'14pt'});
        }
    });
});
/*
~ Copyright (c) Summit Learning Management System (made by students, for students). 2020.
*/

html > body {
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
font-family: "Trebuchet MS", sans-serif;
}

... (CSS Code continues)

<!doctype html>
<!--
~ Copyright (c) Summit Learning Management System (made by students, for students). 2020.
-->
<html lang="en-AU"><head>
<meta charset="UTF-8>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0">
... (Head Content continues)

Answer №1

One way to achieve this is by checking the key event, specifically if the keycode is 80 and the altkey is pressed, then trigger a click event on the menu.

if (e.altKey && keyCode == 80) 

Here's the code snippet:

// Toggle Page List
$(document).ready(function() {
    $('.togglePL').click(function(e) {
        if ($('#pageList').width() == 40) {
            $('.textPL').show();
            $('#pageList').width(168);
            $('.togglePL').css({left:'192px', transform: 'none'});
             $('#pageList a').css({fontSize:'10pt'});
        }
        else {
            $('.textPL').hide();
            $('#pageList').width(40);
            $('.togglePL').css({left:'64px', transform: 'rotate(180deg)', transitionDuration:'0.3s'});
            $('#pageList a').css({fontSize:'14pt'});
        }
    });
});

$(document).on('keydown', function (e) {
        let keyCode = e.keyCode | e.which;
        if (e.altKey && keyCode == 80) {
           $('.togglePL').trigger('click')
        }
      })
/*
~ Copyright (c) Summit Learning Management System (made by students, for students). 2020.
*/

html > body {
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
font-family: "Trebuchet MS", sans-serif;
}

#wrapper {
display: flex;
flex-wrap: nowrap;
height: 100%;
min-height: 100vh;
overflow: hidden;
background: #555;
}
... (CSS styling omitted for brevity)
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What to do when IE6/IE7 margins disappear after moving a relatively positioned floated div using jQuery's .hover()?

Sharing my code with you: http://jsbin.com/uhera3/edit Encountered an issue where the relatively positioned floated divs move to the left in IE7 when using jQuery's .hover() function. Any insights on how to resolve this problem? ...

Executing multiple JQuery post requests simultaneously

I am currently working with three functions, each of which posts to a specific PHP page to retrieve data. However, since each PHP script requires some processing time, there is a delay in fetching the data. function nb1() { $.post("p1.php", { ...

Leveraging an AngularJS variable within an iframe

Is it possible to use a variable inside an iframe src or ng-src attribute? I've tried different variables but none seem to be recognized. For example: <iframe ng-src="http://www.example.com/?name={{test}}"> </iframe> The variable test ju ...

enhancing angular directives with data binding while replacing HTML content inside the compile function

I am currently attempting to develop a directive that will substitute an input field with a custom-made input field. Unfortunately, I am encountering challenges with getting the data binding to function properly, as the model does not display in the direct ...

Display HTML element within the <samp> tag using PHP

I'm using Bootstrap for my form control, but my input and button are not on the same line. <div class="form-group"> <label class="col-lg-3 control-label" id="title-meaning">Meaning:</label> <div c ...

Creating unique styles for components based on props in styled MUI: A comprehensive guide

One challenge I am facing is customizing the appearance of my component based on props, such as the "variant" prop using the 'styled' function. Here is an example code snippet: import { styled } from '@mui/material/styles'; const Remov ...

What is the order of reflection in dynamic classes - are they added to the beginning or

Just a general question here: If dynamic classes are added to an element (e.g. through a jQuery-ui add-on), and the element already has classes, does the dynamically added class get appended or prepended to the list of classes? The reason for my question ...

Customize Your Lightbox Fields with DHXScheduler

In my JSP application for exam timetabling, I have integrated DHXScheduler and customized the lightbox to display additional information about exams using my own fields. The EventsManager class is responsible for managing events by saving, creating, and r ...

Tips for creating a dynamic variable for an object key in jQuery to streamline your code

Utilizing the TweenMax library, I am adding styles to a div while scrolling. However, I am facing a challenge where I need different styles based on varying page resolutions. To address this issue, I implemented an if condition. Is there a way I can use a ...

Utilize the HTML canvas to sketch on an image that has been inserted

I have a HTML canvas element within my Ionic application. <canvas id="canvas" color="{{ color }}" width="800" height="600" style="position:relative;"></canvas> Within this canvas, I am loading an image. Below is the code snippet from the con ...

The data type 'unknown' cannot be assigned to the type 'any[]', 'Iterable<any>', or (Iterable<any> & any[])

I have been working on creating a custom search filter in my Angular project, and it was functioning properly. However, I encountered an error in my Visual Studio Code. In my previous project, everything was working fine until I updated my CLI, which resul ...

The AjaxPoller object is not defined and causing a TypeError

I have a piece of JavaScript code that handles AJAX requests and updates the DOM: this.AjaxHandler = { sendRequest: sendRequest, fetchDataForElement: fetchDataForElement, handleJsonResponse: handleJsonResponse, checkProgress: checkProgress }; fun ...

Struggling to grasp the concept of DOM Event Listeners

Hello, I have a question regarding some JavaScript code that I am struggling with. function login() { var lgin = document.getElementById("logIn"); lgin.style.display = "block"; lgin.style.position = "fixed"; lgin.style.width = "100%"; ...

Encountering a "DOM Exception 11: InvalidStateError" when attempting to use websocket.send

I encountered the following error message: DOM Invalidate exception 11 This error is coming from the code snippet below, but I'm having trouble identifying the root cause. /*The coding style may appear pseudo-stylish with potential syntax errors*/ ...

Avoid displaying duplicate items from the array

Utilizing react js, I am attempting to iterate through an array of objects and display the name of each object in the array: const arr = [ { name:"Bill", age:88 }, { name:"Bill", age:18 }, { name:"Jack", age:55 }, ] ...

Navigating Through Grid and Card Content in React

My goal was to minimize the amount of code by incorporating a reusable component into my application. I am facing an issue on how to iterate through the columns and rows in Grid, Card, and Table. What would be the optimal solution for this problem? Please ...

Troubleshooting and Fixing AJAX Calls

When working with Asynchronous JavaScript, it is common to encounter issues where we are unsure of the posted request and received response. Is there a simple method for debugging AJAX requests? ...

Utilizing AMAZON_COGNITO_USER_POOLS in conjunction with apollo-client: A comprehensive guide

Struggling to populate my jwtToken with the latest @aws-amplify packages, facing some challenges. Encountering an error when attempting to run a Query: Uncaught (in promise) No current user It seems that when using auth type AMAZON_COGNITO_USER_POOLS, I ...

What is the best way to invoke a PHP function using a jQuery AJAX request?

Similar Question: How can I use jQuery $.ajax to trigger a specific PHP function? I am trying to execute a particular PHP function from within my PHP file. Is there a way to reference that function using a jQuery AJAX call instead of invoking the enti ...

Difficulty arise when AJAX array interacts with MySQL database table

One of the challenges I'm facing on my website involves a series of buttons that correspond to specific table columns in a MySQL database. Both the button IDs and column names are identical. My goal is to gather these IDs into an array, then use AJAX ...