Utilizing tag keys for inserting text and adjusting text sizes within a Web application

Creating an online editing interface for coursework where keyboard events are used. The goal is to have the tab key insert text, while also reducing the size of the text on that line. However, upon using getElementById, an error message pops up stating: Cannot read property 'style' of null. Any suggestions on how to tackle this issue?

<!DOCTYPE html>
<html>

<head>
  <title>Editing Tool</title>
  <link rel="stylesheet" href="stylesheet.css">
  <link rel="shortcut icon" href="">
</head>

<body>
<aside class="bar">
  <div id="heading-container"></div>
</aside>
  <div id="inputArea">
    <div id="bulletPoints" contenteditable="true">
      <div id="divList" contenteditable="true">
        <ul class="inputList">
          <li id="outlineList"></li>
        </ul>
      </div>
    </div>
  </div>
  <script>
     window.onload = () => {
     window.addEventListener("keydown", checkKeyPress);

      function checkKeyPress(key){
         if (key.keyCode === 9){
         key.preventDefault();
         document.execCommand("insertText", true, "    ");
         document.getElementById("inputList").style.fontSize = "smaller";
        }
       }
     };
  </script>
</body>
</html>



Answer №1

<!DOCTYPE html>
<html>

<head>
  <title>Editor</title>
  <link rel="stylesheet" href="stylesheet.css">
  <link rel="shortcut icon" href="">
</head>

<body>
<aside class="bar">
  <div id="heading-container"></div>
</aside>
  <div id="inputArea">
    <div id="bulletPoints" contenteditable="true">
      <div id="divList" contenteditable="true">
        <ul class="inputList" id="inputList">
          <li id="outlineList"></li>
        </ul>
      </div>
    </div>
  </div>
  <script>
     window.onload = () => {
     window.addEventListener("keydown", checkKeyPress);

      function checkKeyPress(key){
         if (key.keyCode === 9){
         key.preventDefault();
         document.execCommand("insertText", true, "    ");
         document.getElementById("inputList").style.fontSize = "smaller";
        }
       }
     };
  </script>
</body>
</html>

Your ul element is missing the id attribute.

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

Matching stroke-dashoffset in SVG between two distinct paths

I am currently working on animating stroke-dashoffset for two different paths to create a drawing effect. There are buttons provided to adjust the stroke-dashoffset values. My goal is to ensure that the filled paths align vertically as they progress. Is t ...

Switch between Fixed and Relative positions as you scroll

Just a small adjustment is needed to get it working... I need to toggle between fixed and relative positioning. JSFiddle Link $(window).on('scroll', function() { ($(window).scrollTop() > 50) ? ($('.me').addClass('fix ...

Utilize HTML5 to enable fullscreen functionality for embedded SWF files

I have implemented a function that handles the click event of a button to toggle a DOM element into fullscreen mode. The function works well, but I am facing an issue when there is another div containing a swf inside the main div. var elem = document.getE ...

The appearance of the pop-up mask is displayed at lightning speed

Check out the demo here Here is the HTML code: <div class="x"> </div> <input class="clickMe" type="button" value="ClickMe"></input> This is the JS code: $(".clickMe").click( function() { ...

Pass the callback function `cb_func` as a parameter to the `foobar` function when calling it in the `onclick`

I have an element that I want to trigger an onclick event with a callback function. Here is the code snippet: function cb_func() { alert("hello world!"); } function foobar(i, cb) { // do something if (cb != undefined) cb(); } onclic ...

JavaScrip $("").text(); is a straightforward way to recognize and extract

At the moment, I am utilizing the jQuery script below: $("TD.info > font").text(); when this specific HTML structure is present on a webpage: <td class="info"> <font> 3001474535 </font> </td> I had the idea to tweak t ...

How to trigger a hover effect on a div and its child simultaneously using HTML and jQuery

Here's my concept: I want the text to be contained within div elements with an integrated image, rather than just having fading in and out pictures. Here's my attempt: #first{ position: absolute; } #second{ position: absolute; -we ...

Is there a way to modify the text of image URLs using JavaScript?

My method of replacing a specific word in text works like this: document.body.innerHTML = document.body.innerHTML.replace(/katt/g, "smurf"); However, when I try to replace an image URL in HTML using the same line of code, it doesn't seem to work. H ...

Angular and Bootstrap4: The Perfect Duo for Modals

I need to display a Bootstrap4 modal window in Angular when a specific condition is met in my "bookTour" method without the need for a direct button click. How can I achieve this? Below is the code snippet: html <div class="modal" id="myModal" [ngClass ...

The click-handler method in VueJS Paginate Component fails to activate

I'm currently working on a Vue Component code that involves pagination for a list. The pagination seems to be working fine, except for the issue I encounter when trying to navigate to the next page, which in this case is page 2. I've added a cons ...

Title remains consistent | Angular 4

Struggling to change the document title on a specific route. The route is initially set with a default title. { path: 'artikel/:id/:slug', component: ArticleComponent, data: {title: 'Article', routeType: RouteType.ARTICLE, des ...

What is the best way to ensure that an anchor in a scrolling list adjusts its width based on its children's content?

Check out the setup I have here: http://jsfiddle.net/9F6CF/ In this scenario, there is a list containing anchors within each list item. The UL element has a specific width and an overflow set to auto, causing text in the anchors to scroll horizontally if ...

An unforeseen repetition of jQuery Ajax calls

I'm currently working on an application that utilizes ajax calls to load content. However, I've encountered a situation where an ajax call goes into a loop but seems to end randomly. Below is the code sequence starting from a double click event l ...

What is the best way to assign a Python variable to an <a-assets> element?

There exists a Python function that provides the chosen background <a-sky ID="sky" color="{{object.background}}"></a-sky> The backgrounds can be in the format of '#c6aa99', '#e1a600', '#290942', 'star' ...

Creating a see-through effect for dijit.Dialog as it appears

Utilizing the dijit.Dialog library, I have implemented a Dojo Dialog box that appears on a Rowclick event of a Dojo grid. I am looking to make the Dialog box transparent or reduce its opacity so that the background is visible and it looks visually appealin ...

My React app experienced a severe crash when trying to render an animation in L

Currently, I am working on a React application that was set up using Vite. I recently incorporated an animation using Lottie, and although I was successful in implementing it, I encountered a problem when navigating between different pages of my applicati ...

Using TypeScript to type styled-system props

Currently, I am utilizing styled-system and one of the main features of this library is its shorthand props that allow for simple and quick theming. Although I have streamlined my component, a significant aspect lies here: import React from 'react&a ...

angular.js:13920 Alert: [ngRepeat:dupes] Multiple occurrences in a repeater

I encountered an issue while attempting to parse a JSON file and map it in HTML. Here is the JavaScript code snippet: searhController.orderlogs.results = JSON.stringify(response.data); This is how it's implemented in Angular: <tr ng-hide="searh ...

PHP/AJAX user action history manager

Is there a library available that offers undo/redo functionality with a complete history for a web application? One possible solution could be a system using php/javascript/ajax where you can record the opposite action and variable state for each user acti ...

Vuetify's autofocus feature is limited to functioning only when the modal is first opened

When attempting to utilize Vuetify's v-text-field with the autofocus attribute, I am facing an issue where it only works the first time. Once I close the dialog, the autofocus functionality no longer works. This is the code snippet I am trying to imp ...