The error occurred because 'form' was not properly defined in the loadEventListeners function, resulting in

I can't seem to figure out why I keep encountering this error message. It keeps saying that my loadEventListeners is not defined, but I'm unsure why this would be the case. Any advice or suggestions on this matter would be greatly appreciated.

//Setting up variables for UI elements
const form = document.querySelector('#post-form');
const postList = document.querySelector('.collection');
const clearBtn = document.querySelector('.clear-posts');
const filter = document.querySelector('#filter');
const postInput = document.querySelector('#post');

// Call function to load all event listeners
loadEventListeners();

//Function to load all event listeners
function loadEventListeners(){
  //Add Event Listener for submitting a post
  form.addEventListener('submit', addPost);
}

//Function to add a new post
function addPost(e){
  if(postInput.value === ''){
    alert('Please add a post before submitting');
}

  //Creating li element
  const li = document.createElement('li');
  //Adding class
  li.className = 'collection-item';
  //Creating text node and appending it to li
  li.appendChild(document.createTextNode(postInput.value));
  //Creating a delete link element
  const link = document.createElement('a');
  //Adding class
  link.className = 'delete-item secondary-content';
  //Adding icon HTML
  link.innerHTML= '<i class="fa fa-remove"></i>';
  //Appending the link to li
  li.appendChild(link);

  //Appending li to ul
  console.log(li);

  e.preventDefault();
}

Answer №1

Spotting a small error on the opening line: It ought to say form instead of from.

const form = document.querySelector('#post-form');

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

"Learn how to pass around shared state among reducers in React using hooks, all without the need for Redux

I've built a React hooks application in TypeScript that utilizes multiple reducers and the context API. My goal is to maintain a single error state across all reducers which can be managed through the errorReducer. The issue arises when I try to upd ...

Is the custom attribute event being triggered too soon?

My Unique Component Creation Journey I have meticulously crafted a custom component to enhance the navigation of my application. The core structure consists of an ul element, with each li item dynamically generated based on the contents of the router&apo ...

I encountered an issue while using mediaRecorder to save a JavaScript animation; the blob value returned as null

My current project involves using Vanta js to create an animated background and mediaRecorder to capture the canvas as a webm file. The recording process is supposed to start from the beginning and stop after 1 second, exporting the webm file. However, I ...

Having trouble selecting an element by name that contains a specific string followed by [..] using jQuery

I have elements with names like kra[0][category], kra[1][category], and so on. However, I am facing difficulties in selecting these elements by name. Here is my jQuery code: $("[name=kra[0][category]]").prop("disabled", true); ...

Trigger the execution of a Python script through a webpage with just the click of a button

I have a small web interface where I need to control a Python script that is constantly gathering data from a sensor in a while loop. Ideally, I would like the ability to start and stop this script with the click of a button. While stopping the script is s ...

The AJAX function fails to trigger the MVC controller method

I'm attempting to enable inline editing by cell, rather than by row, when double-clicking. Although the initial setup is working, it's not updating the record as expected - the "SaveCustomer" call to the controller isn't triggered. Can anyon ...

React cannot be utilized directly within HTML code

I am looking to incorporate React directly into my HTML without the need for setting up a dedicated React environment. While I can see the test suite in the browser, my React app fails to load. Below is the content of my script.js file: I have commented ...

Creating multiple "read more" and "read less" buttons on a single webpage

Can you figure out what's going wrong here? I assigned each one a separate ID, but it's still not functioning properly in my code. I need to get this fixed for an assignment that's due in a few days, so any help is appreciated! function r ...

Unable to trigger @click event on nuxt-link component within Nuxt 3

As per a Nuxt 2 question on Stack Overflow, it was suggested that the following code should prevent nuxt-link from navigating to a new page: <nuxt-link :to="`/meet`" class="group font-normal" @click.native="event => event.pre ...

simulating interaction with databases within API routes

I am currently working on developing a full stack application using NextJS along with a MySQL database. Within my API routes, I interact with this database by making calls to various functions such as createOne(), which is responsible for creating new inst ...

The Redux dispatch function enters a loop when the success flag is set to true within the state

Currently, my Redux state is structured as follows: const initialState = { data: [], loading: false, success: false, error: null, }; I have implemented a CRUD API in the node backend. In my React application, I have two components: The first com ...

Is there a way for me to extract the error message generated by an API request?

I am currently working on an authentication project and I am facing a challenge in displaying error messages on the screen. Specifically, I am unsure about how to show error messages such as when an email address is already registered. While I can see th ...

Press the button to copy the content to the clipboard with JavaScript

I am struggling with copying email text in HTML within an href tag. I want the email value to be copied when a user clicks on the icon next to it, but for some reason it is not working as expected. Instead of just copying the email address, my code seems ...

Jquery button click event is malfunctioning after the inclusion of jquery keyboard plugin

When I try to gather user input from a textbox using jQuery keyboard.js, the functionality works perfectly fine if I exclude the virtual keyboard code. For more information on keyboard.js, you can visit: https://github.com/Mottie/Keyboard/wiki Below is t ...

Error: The function row.child.hasClass is not a recognized function

My challenge is creating row details for DataTables without using Ajax. I have implemented something like this: $(document).ready(function () { function format ( name, value ) { return '<div>Name: ' + name + '<br /> ...

What sets {props.children} apart from conventional objects?

Currently, I am deep diving into React by taking a crash course. The topic at hand is nested components and props, which has left me slightly confused. In my attempt to create a simple comment section, similar to thishttps://i.sstatic.net/7uNlj.png ...

Ways to alter a PHP variable with each AJAX request

Utilizing AJAX to dynamically add more content to an image gallery. I'm passing a PHP variable to dictate the current page number and other necessary data. Each time new data is fetched via AJAX, I want to increment the page variable by one to fetch ...

After manipulating the array, Vue fails to render the input fields generated by the v-for directive

After setting the value externally, Vue component won't re-render array items. The state changes but v-for element does not reflect these changes. I have a component that displays items from an array. There are buttons to adjust the array length - &a ...

Using vue-select: In VueJs, how to pass an option slot from a grandparent component

Utilizing a custom dropdown component called 'vue-select', there is an option to customize the options view using slots as detailed in this documentation -> https://vue-select.org/guide/slots.html I am aiming to achieve a similar customizatio ...

What is the best method for choosing elements when I already possess a DOM element?

When you have a variable that contains a DOM element and need to select related elements, you can easily achieve this by wrapping it in a jQuery object. var myDomElement = document.getElementById("foo"); $(myDomElement ).find("a"); It is common for peopl ...