While attempting to code a pop up search bar with HTML, CSS, and JavaScript, I encountered an issue where an error was displayed on the console

<form action="" class="search-form">
    <input type="search" id="search-box" placeholder="search here...">
    <label for="search-box" class="fa fa-search" aria-hidden="true"></label>
</form>

My javascript code:

const form = document.querySelector('.search-form');

document.querySelector('#search-b t n').onclick = () => {
    form.classList.toggle('active');
}

An error message occurred:

Uncaught TypeError: Cannot set properties of null (setting 'onclick')
    at script.js:5

Answer №1

Initially, your code appears to be quite peculiar and contains excessive spacing. I have made adjustments to remove the unnecessary spaces, but please ensure that all functions are working correctly.

The error occurred because it could not locate the object with the ID search-btn. Even after removing the space, the issue persists because there is no corresponding div with this ID in your form.

I recommend using the following code instead:

document.querySelector('#search-box`)

This ID does indeed exist within your document structure.

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

Encountering issues with MyModel.findOne not being recognized as a function while utilizing Mongoose within Next.js 14 Middleware

Within my Next.js middleware, I am encountering the following code: let user: UserType[] | null = null; try { user = await findUser({id}); console.log("user", user); } catch (error: any) { throw new Error(error) ...

Guide to generating dynamic multiple fields in AngularJS

I have an AngularJS app where I am adding multiple dynamic fields within a form. Here is how I have tried to do it: The JavaScript file looks like this: $scope.commonData = [{ 'a': '', 'b': '', }]; $scope. ...

Ways to retrieve a file from a specific location using fetch/axios?

For my research, I need to utilize certain network APIs such as fetch or axios to access a local file without using the fs module or importing them directly. I attempted to use both fetch and axios but found that they do not support fetching local files, ...

How can one locate the Unicode values for subscript letters in the alphabet?

While I have uncovered a few letters, my search continues for the elusive "c", "m", and "p". Is it possible to locate them as well? ...

Issues with Pen Points and Groupings

After receiving json data, my goal is to populate a map with markers that will cluster together if they are located too closely. To achieve this, I am utilizing specific extensions for Markers and Clusters available at this link. I have written code that ...

What is the process for defining functions with distinct data types while allowing variables to have multiple data types?

I am facing a declaration issue - or rather, a challenge in comprehending Typescript. Let me illustrate the scenario: public migrationSource: Skater | Rink; public migrationDestination: Skater | Rink; public migrationMode: MigrationMode; ngOnInit() { ...

Tips for handling mismatched function parameters in TypeScript when using an unspecified data type

Even though I wish it wasn't the case, TypeScript accepts the code below in strict mode. The function's value argument is defined as either an unknown or an any type, meaning it can be anything at this point as it is being passed along. However, ...

Determine the absolute path with respect to a different reference path, rather than the current working directory

One of the challenges I'm facing with my Node.js module is how to easily allow developers to edit the location of the dependencies relative to the module file itself. The issue arises because the current working directory, accessed through `process.cw ...

What is the method to store and retrieve data attributes linked to elements such as select options within the React framework?

Despite my efforts, I'm currently unable to retrieve the data attribute from an option using React as it keeps returning null. <select onChange={(e) => this.onIndustryChangeOption(e)} value={this.props.selectedIndustry}> <opti ...

What is preventing me from "importing" react-dom.js?

Implementing ReactDOM into my Jest tests has been a bit of a challenge. Let's take a look at the code snippet below. const React = require('../src/js/vendor/react/build/react.js'); const ReactDOM = require('../src/js/vendor/react/build ...

Angular 8 fails to retain data upon page refresh

I have a property called "isAdmin" which is a boolean. It determines whether the user is logged in as an admin or a regular user. I'm using .net core 2.2 for the backend and Postgre for the database. Everything works fine, but when I refresh the page, ...

Experiencing the swift blink of secure route's content using React Router 6

After implementing a "protected route" using React Router 6, I noticed that the protected page's content briefly flashes before redirecting to the login page. Is there a known solution to this issue? Here is the code for the ProtectedRoute component: ...

Sending information from JavaScript to Pug templateorTransferring

I'm currently experimenting with Node.js and MongoDB within an Express framework. My current goal is to achieve the following: Establish a connection to MongoDB and retrieve a document from a specific collection (which I have successfully done). Pr ...

The Flowbite CSS plugin seems to be malfunctioning when attempting to include it within the tailwind.config.js file

Incorporating Flowbite (both JS and CSS) into my application has been a bit of a challenge. I managed to successfully insert the JS using Webpack and import 'flowbite' in the entry point JS file. As for the CSS, I followed the documentation and ...

Issue with CSS not loading correctly when switching routes in AngularJS using Gulp and Router

I currently have a router set up in my application: var mainmodule.config(function($routeProvider){ $routeProvider.when('/home',{ templateUrl:'src/home/home.html' }; $routeProvider.when('/home/sports',{ ...

Why isn't the Full Calendar loading automatically within a Bootstrap Tab?

I have been working on a travel website and incorporated a bootstrap tab feature. In the first tab, I have some content, while in the second tab, I've added a full calendar with JavaScript. Everything seems to be functioning correctly when the full ca ...

Submit Form using Ajax - Update text in HTML element upon click

As I am working on developing a message system, I encountered an issue where the message content is not opening correctly in my template when clicking the "See" button. My intention is to implement an Ajax call that will replace the content with jQuery .te ...

Monitoring VueFire for an empty array attribute

Seeking a resolution to my current issue! I am attempting to integrate Vue.Draggable with VueFire. I have multiple lists with draggable cards that can be moved between them, sorted, duplicated, and more. When the lists have cards populated, Vue.Draggable ...

Executing JavaScript upon inserting it into the HTML of a webpage

On a webpage, I have content that is first sent from the server at page load and then frequently updated via AJAX. To handle this, I currently use $( function () {} ) for binding and updating based on the initial server data. However, I also need a way to ...

Using JavaScript to open links in a new tab with the target

document.getElementById("mahacareer").onclick = function () { window.open("http://www.mahacareermitra.in", '_blank'); }; <a href="" id="mahacareer">Access the portal</a> Hi there, I am looking to have the link above open in a new tab ...