CSS in HTML not functioning as expected without any errors

I watched a YouTube tutorial and discussed the code with others before running it through syntax error programs. Despite passing CLLlint and the w3validator, the menu still doesn't function as intended.

$(document).ready(function() {
  $('sidebar-btn').click(function() {
    $('#sidebar').toggleClass('visible');
  });
});
body {
  margin: 0;
  padding: 0;
  font-family: "Helvetica Neue", helvetica, arial, sans-serif;
}

.sidebar {
  background: #33676767;
  width: 200px;
  height: 100%;
  display: block;
  position: absolute;
  left: -200px;
  top: 0;
  transition: left 0.3s linear;
}

.sidebar.visible {
  left: 0;
  transition: left 0.3s linear;
}

ul {
  margin: 0;
  padding: 0;
}

ul li {
  list-style: none;
  text-align: center;
}

ul li a {
 /* color: white; */
  width: 180px;
  padding: 10px;
  text-decoration: none;
  font-size: 18px;
}

.sidebar-btn {
  display: inline-block;
  vertical-align: middle;
  width: 20px;
  height: 15px;
  cursor: pointer;
  margin: 20px;
  position: absolute;
  top: 0;
  right: -50px;
}

.sidebar-btn span {
  height: 1px;
  background: #66CC33;
  margin-bottom: 5px;
  display: block;
}

.sidebar-btn span:nth-child(2) {
  width: 75%;
}

.sidebar-btn span:nth-child(3) {
  width: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<div class="sidebar">

  <ul>
    <li><a href="/index.html">Home</a></li>
    <li><a href="/index.html">Sweet</a></li>
    <li><a href="/index.html">Home</a></li>
    <li><a href="/index.html">All</a></li>
    <li><a href="/index.html">4One</a></li>
  </ul>

  <div class="sidebar-btn">
    <span></span>
    <span></span>
    <span></span>

  </div>

</div>

Answer №1

Make sure to correct your jquery selector - you have mistakenly used a class instead of an ID. Here is the corrected code snippet:

$(document).ready(function(){
    $('.sidebar-btn').click(function(){
       $('.sidebar').toggleClass('visible'); 
    });           
});

Take note of the '.' in front of 'sidebar-btn' which was missing previously - this signifies a selector for a class. Also, notice the use of '.' instead of '#' before 'sidebar', indicating a class selector rather than an ID selector.

Answer №2

There are a few issues with your code that need fixing:

  • The script tag is not properly closed
  • The meta tag is also missing the closing tag
  • You are targeting the sidebar as an ID but it's defined as a class
  • The jQuery selector for sidebar-btn needs a leading . to denote it's a class

Here is a corrected version of your code - I have commented out the white color for demonstration purposes:

body 
{
    margin: 0;
    padding: 0;
    font-family:"Helvetica Neue", helvetica, arial, sans-serif;
}

.sidebar
{
    background: #33676767;
    width: 200px;
    height: 100%;
    display: block;
    position:absolute;
    left: -200px;
    top: 0; 
    transition:left 0.3s linear;
}

.sidebar.visible
{
    left:100px;
    transition:left 0.3s linear;
}

ul
{
    margin: 0;
    padding: 0;
}

ul li
{
    list-style: none;
    text-align: center;
}

ul li a
{
   /* color: white; */
    width: 180px;
    padding: 10px;
    text-decoration: none;
    font-size: 18px;
}

.sidebar-btn
{
    display: inline-block;
    vertical-align: middle;
    width: 20px;
    height: 15px;
    cursor: pointer;
    margin: 20px;
    position:absolute;
    top: 0;
    right: -50px;

}

.sidebar-btn span
{
    height: 1px;
    background: #66CC33;
    margin-bottom: 5px;
    display: block;
}

.sidebar-btn span:nth-child(2)
{
    width: 75%;
}

.sidebar-btn span:nth-child(3)
{
    width: 50%;
}
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="home.css">
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>

<div class="sidebar" id="sidebar">

<ul>
    <li><a href="/index.html">Home</a></li>
    <li><a href="/index.html">Sweet</a></li>
    <li><a href="/index.html">Home</a></li>
    <li><a href="/index.html">All</a></li>
    <li><a href="/index.html">4One</a></li>
</ul>

<div class="sidebar-btn">
    <span></span>
    <span></span>
    <span></span>

</div>

</div>

<script>

$(document).ready(function(){
    $('.sidebar-btn').click(function(){
       $('#sidebar').toggleClass('visible'); 
    });           
});

</script>

</html>

Answer №3

I haven't had a chance to try it out, but I noticed that you missed adding a "." before sidebar-btn for the class selector and #sidebar is mistakenly defined as an id instead of a class in the HTML.

Here's a suggestion:

$(document).ready(function(){
    $('.sidebar-btn').click(function(){
       $('.sidebar').toggleClass('visible'); 
    });           
});

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 is the most effective method for retrieving a PHP return value using Ajax?

As I work on creating a validation form in Ajax and PHP, I find myself unsure of how to fetch values from PHP. Can anyone guide me through this process? For instance: The validation form exists in index.php while the page containing the function is check ...

How can I retrieve the current background color and revert it back after a .hover event?

I am trying to use the .hover function to change the background color of a menu. Each menu item has a different background color, so I want it to return to its original color when the user hovers off. In other words, I want the script to read the current b ...

Customizing the Default Placeholder Appearance in Bootstrap Date Range Picker

HTML: <input type="text" name="form" placeholder="Check In"> JS: $('input[name="form"]').daterangepicker({ singleDatePicker: true, showDropdowns: true, }); This is how I want it to appear. view example Find more information on ...

"Encountered a 404 error while making an AJAX call in

Utilizing ajax to gather information from an enquiry form, the data is then forwarded to another page which should in turn send this data to my email. The issue arises when clicking the send button as a 404 error is displayed in the firebug console. Wi ...

Tips for presenting JSON information in an HTML modal box

I have a json data array that I need to display in an HTML modal. Currently, the json data array is being displayed correctly in the console. Javascript $('.detailButton').on('click', function(){ var BASE_URL = "http://localhost/empl ...

Email link spilling out from within image hyperlink

My website, located at , is experiencing an issue where the small envelope icon's mailto link is overflowing to the whole box instead of just the image itself. If anyone could provide assistance in resolving this issue, it would be greatly appreciate ...

Tips for sending CSS and HTML elements back to the client's browser after a Form-POST action and JavaScript

I am currently working on a project to create a platform where users can easily make payments to each other. I aim to simplify the process using JavaScript and HTML Forms, similar to how Stripe offers Checkout buttons (as seen in the sample code below). & ...

Error code 302 is triggered when attempting to retrieve an HTML file from a web address

I'm having trouble retrieving the HTML document from the URL provided below: The issue is that I keep getting a 302 response code! I am not very familiar with how this request is being handled (with the function and parameters), so I am unsure of the ...

Centering an image and text both vertically and horizontally within a container div

I am currently working on creating a series of thumbnail-style divs with fixed dimensions. These divs will contain an image and a title text, both of which need to be centered vertically and horizontally within the container div. While I have successfully ...

In a trivia game implemented in JavaScript, only the initial response from an array of objects will be considered as true, instead of focusing on the correct answer

Currently, I am working on a trivia game with four answer buttons. Oddly, the condition only returns as true when the first answer button (index 0) is clicked, even if it is actually false. I suspect there might be a bug in the "answer" variable somewher ...

Retrieving information using an ajax request in JavaScript

Although this question may have been asked several times before, I have yet to find a satisfactory answer. I passed a URL in an Ajax call and I am trying to retrieve data from the database through a query in the success method of the Ajax request, but for ...

Creating a selection area with CSS that appears transparent is a straightforward process

I'm currently exploring ways to implement a UI effect on a webpage that involves highlighting a specific area while covering the rest of the page with a semi-transparent black overlay, all using CSS only. What is the most common approach to achieving ...

Solve inconsistent divs using HTML and Bootstrap styling techniques

I'm currently working on a .cshtml page where I display database objects in a card format using HTML and Bootstrap. However, the cards are appearing unevenly as shown below: https://i.sstatic.net/LbrrH.png Here is the code snippet I am using: @model ...

Leveraging Angular 8's ngIf directive in combination with the async pipe to dynamically display or hide HTML elements

I've been attempting to dynamically show and hide HTML elements based on the result of a service call. I have tried using *ngIf="messageService.getData() | async", but it doesn't seem to be working as expected. With the async, the "Failure Messag ...

What is the best way to ensure an image fits perfectly within a div container

I'm attempting to design a straightforward card layout where the image spans the entire width of the container div. My vision is for it to resemble something like the example in the linked image below. https://i.sstatic.net/X5oJD.png I experiment ...

What is the best way to selectively import specific data from an HTML table into R?

Currently I am working on importing a table from a website to R for my homework assignment. The specific website I am referring to is . I attempted the following code, which seemed fine at first, but it included the description part under the variable "Ins ...

Using JavaScript click event to send information to the database

I really need some guidance with JavaScript and ajax. I would appreciate any help on this topic. I am particularly interested in the question here: Updating a MySql database using PHP via an onClick javascript function Specifically, I am intrigued by th ...

Adjust the container size based on changes in font size

One interesting issue I encountered involves displaying each word in a sentence in separate div elements using inline-block with a max-width of 120px. When attempting to increase the font size within the parent div, the inline-block divs begin to overlap d ...

Guide to ensuring child elements inherit parent's width in Bootstrap 4

Want to ensure that a child element inherits the parent's width in Bootstrap 4? Trying to adjust the width of the download_resume block. Currently using two key Javascript scripts: 1. The first function changes the image with a hover effect 2. The ...

Using CSS to target the last child and hover states of an anchor link within an unordered list

I am currently adding styles to the last child of a navigation menu, and I have been successful with this code: .aston-menu-light ul > li:last-child { border:2px solid blue; border-radius: 50px; padding:0 20px 0 20px; } .aston-menu-light ...