Home > OS >  Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at (index)
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at (index)

Time:12-18

let searchBtn = document.querySelector("#Search-btn");
let searchBar = document.querySelector(".search-bar-container");

searchBtn.addEventListener("click", () => {
  searchBtn.classlist.toggle("fa-times");
  searchBar.classList.toggle("active");
});

let searchBtn = document.querySelector ( '#Search-btn');
let searchBar = document.querySelector ( '.search-bar-container');

searchBtn.addEventListener('click', () =>{

    searchBtn.classlist.toggle('fa-times');
    searchBar.classList.toggle('active');
});
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@200;300;400;600;700&display=swap');

:root {
    --orange:#ffa500;
    
}

*{
    font-family: 'Nunito', sans-serif;
    margin: 0; padding: 0;
    box-sizing: border-box;
    text-transform: capitalize;
    outline: none; border: none;
    transition: all .2s linear;
    text-decoration: none;
    
}

*::selection{
    background: var(--orange);
    color: #fff;
}

html{
    font-size: 62.5%;
    overflow-x: hidden;
    scroll-padding-top: 6rem;
    scroll-behavior: smooth;
}

header{
    position: fixed;
    top: 0; left: 0; right: 0;
    background: #333;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 2rem 9%;
}

header .logo {
    font-size: 2rem;
    font-weight: bolder;
    color: #fff;
    text-transform: uppercase;
}

header .logo span {
    color: var(--orange);
}

header .navbar a{
    font-size: 2rem;
    color: #fff;
    margin:0 .8rem;
}

header .navbar a:hover{
    color: var(--orange);
}

header .icons i{
    font-size: 2.5rem;
    color:#fff;
    cursor: pointer;
    margin-right: 2rem;
}

header .icons i:hover{
    color: var(--orange);
}

header .search-bar-container{
    position: absolute;
    top: 100%; left: 0; right: 0;
    padding: 1.5rem 2rem;
    background: #333;
    border-top: .1rem solid rgba(225, 225, 225, .2);
    display: flex;
    align-items: center;
    z-index: 1001;
    clip-path: polygon(0 0, 100% 0, 0 0);
}

header .search-bar-container.active{
    clip-path: polygon(0 0, 100% 0, 0 0);
}

#search-bar {

    width: 100%;
    padding: 1rem;
    text-transform: none;
    color: #333;
    font-size: 1.7rem;
    border-radius: 100rem;
}

header .search-bar-container label{
    color: #fff;
    cursor: pointer;
    font-size: 3rem;
    margin-left: 1.5rem;
}

header .search-bar-container label:hover {
    color: var(--orange);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Travel Agency</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    
<header>
    <a href="#" > <span>T</span>ravel</a>

    <nav >
        <a href="#home">home</a>
        <a href="#book">book</a>
        <a href="#package">package</a>
        <a href="#services">services</a>
        <a href="#review">review</a>
        <a href="#contact">contact</a>
    </nav>

    <div >
        <i  id="search-btn"></i>
        <i  id="login-btn"></i>
    </div>

    <form action="" >
        <input type="search" id="search-bar" placeholder="search here...">
        <label for="search-bar"  ></label>
    </form>

</header>

When I try to run my javascript code its give me an error. I do check my code and syntax twice i could not find the error please help me with that.

CodePudding user response:

There is nothing called #Search-btn, it's #search-btn. So, your JavaScript should look like

    let searchBtn = document.querySelector ( '#search-btn');
    let searchBar = document.querySelector ( '.search-bar-container');

    searchBtn.addEventListener('click', () =>{

        searchBtn.classlist.toggle('fa-times');
        searchBar.classList.toggle('active');
    });

Next, the toggle function has to be used on classList. But, in this line:

searchBtn.classlist.toggle('fa-times');

You can see that you're calling classlist there. After you fix that, this is what your JavaScript code should look like:

    let searchBtn = document.querySelector ( '#search-btn');
    let searchBar = document.querySelector ( '.search-bar-container');

    searchBtn.addEventListener('click', () => {
        searchBtn.classList.toggle('fa-times', true);
        searchBar.classList.toggle('active', true);
    });

CodePudding user response:

There two problems in your code, first #Search-btn id doen't exist, it should be #search-btn, and the second searchBtn.classlist.toggle('fa-times'); here classlist is wrong, it should be classList, like below code:

let searchBtn = document.querySelector('#search-btn');
let searchBar = document.querySelector('.search-bar-container');

searchBtn.addEventListener('click', () =>{
 searchBtn.classList.toggle('fa-times');
 searchBar.classList.toggle('active');
});

below snippet is exactly you snippet with just fixing two above problems:

let searchBtn = document.querySelector ( '#search-btn');
let searchBar = document.querySelector ( '.search-bar-container');

searchBtn.addEventListener('click', () =>{

    searchBtn.classList.toggle('fa-times');
    searchBar.classList.toggle('active');
});
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@200;300;400;600;700&display=swap');

:root {
    --orange:#ffa500; 
}

*{
    font-family: 'Nunito', sans-serif;
    margin: 0; padding: 0;
    box-sizing: border-box;
    text-transform: capitalize;
    outline: none; border: none;
    transition: all .2s linear;
    text-decoration: none; 
}

*::selection{
    background: var(--orange);
    color: #fff;
}

html{
    font-size: 62.5%;
    overflow-x: hidden;
    scroll-padding-top: 6rem;
    scroll-behavior: smooth;
}

header{
    position: fixed;
    top: 0; left: 0; right: 0;
    background: #333;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 2rem 9%;
}

header .logo {
    font-size: 2rem;
    font-weight: bolder;
    color: #fff;
    text-transform: uppercase;
}

header .logo span {
    color: var(--orange);
}

header .navbar a{
    font-size: 2rem;
    color: #fff;
    margin:0 .8rem;
}

header .navbar a:hover{
    color: var(--orange);
}

header .icons i{
    font-size: 2.5rem;
    color:#fff;
    cursor: pointer;
    margin-right: 2rem;
}

header .icons i:hover{
    color: var(--orange);
}

header .search-bar-container{
    position: absolute;
    top: 100%; left: 0; right: 0;
    padding: 1.5rem 2rem;
    background: #333;
    border-top: .1rem solid rgba(225, 225, 225, .2);
    display: flex;
    align-items: center;
    z-index: 1001;
    clip-path: polygon(0 0, 100% 0, 0 0);
}

header .search-bar-container.active{
    clip-path: inherit;
}

#search-bar {
    width: 100%;
    padding: 1rem;
    text-transform: none;
    color: #333;
    font-size: 1.7rem;
    border-radius: 100rem;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">

<header>
    <a href="#" > <span>T</span>ravel</a>

    <nav >
        <a href="#home">home</a>
        <a href="#book">book</a>
        <a href="#package">package</a>
        <a href="#services">services</a>
        <a href="#review">review</a>
        <a href="#contact">contact</a>
    </nav>

    <div >
        <i  id="search-btn"></i>
        <i  id="login-btn"></i>
    </div>

    <form action="" >
        <input type="search" id="search-bar" placeholder="search here...">
        <label for="search-bar"  ></label>
    </form>

</header>

  • Related