Navbar collapse not functioning properly on HTML, CSS, and JavaScript

I have developed a CSS code snippet:

.nav{
    right: 0px;
    left: 0px;
    margin-top: 0px;
    margin-bottom:20px;
    height: 35px;
    text-align: center;
    background-color: red;
    z-index: 1;
}

.sticky {

background: #000;
text-align: center;
margin: 0 auto;
position: fixed;
z-index: 9999;
border-top: 0;
top: 0px;
font-size: 17px;
box-shadow: 0px 0px 3px 0px #949494;
background: #10b5fa;
padding-top: 6px;

} 
.accordion-section-title {

width:100%;
padding:10px;
display:inline-block;
border-bottom:1px solid #1a1a1a;
background:#333;
transition:all linear 0.15s;
/* Type */
font-size:1.200em;
text-shadow:0px 1px 0px #1a1a1a;
color:#fff;
text-decoration: none;
}

.accordion-section-title.active, .accordion-section-title:hover {
    background:#4c4c4c;
    /* Type */
    text-decoration:none;
}



/*----- Section Content -----*/
.accordion-section-content {
    padding:10px;
    display:none;
}

Additionally, I have included JavaScript in my project:

jQuery(document).ready(function() {
function close_accordion_section() {
    jQuery('.accordion .accordion-section-title').removeClass('active');
    jQuery('.accordion .accordion-section-content').slideUp(300).removeClass('open');
}

jQuery('.accordion-section-title').click(function(e) {
    // Grab current anchor value
    var currentAttrValue = jQuery(this).attr('href');

    if(jQuery(e.target).is('.active')) {
        close_accordion_section();
    }else {
        close_accordion_section();

        // Add active class to section title
        jQuery(this).addClass('active');
        // Open up the hidden content panel
        jQuery('.accordion ' + currentAttrValue).slideDown(300).addClass('open'); 
    }

    e.preventDefault();
});
});

Lastly, here is the HTML code I used:

<nav class="nav sticky">

            <a class=".accordion-section-title" href="#tujuan"> Kategori</a>
            <div id="tujuan" class="accordion-section-content">
                <p>Hello World</p>
            </div>

        </nav>

I am facing an issue where the ID "tujuan" does not work as expected to show the accordion effect. I referred to this source for guidance on how to implement it:

Would appreciate any help or insights on resolving this matter.

Answer №1

Upon reviewing the code, it is important to note that the line:

<a class=".accordion-section-title" href="#tujuan"> Kategori</a>
should have the period removed in front of ".accordion".

Therefore, it should be modified to: -->

<a class="accordion-section-title" href="#tujuan"> Kategori</a>

Remember to use the syntax ".classname" only when referencing classes outside of HTML, such as in CSS or JS. In HTML, simply type the class name without any special characters.

If you provide a jsfiddle link, I can offer further assistance with troubleshooting. Best of luck with your coding endeavors!

Sincerely, CB

Answer №2

It seems like there are some issues with your HTML code and jQuery function. You have set the jQuery function to target the class .accordion but in your code, you are actually using the class .nav.

Please refer to the example below for clarification.

jQuery(document).ready(function() {
  function close_accordion_section() {
    jQuery('.nav .accordion-section-title').removeClass('active');
    jQuery('.nav .accordion-section-content').slideUp(300).removeClass('open');
  }

  jQuery('.accordion-section-title').click(function(e) {
    // Grab current anchor value
    var currentAttrValue = jQuery(this).attr('href');

    if (jQuery(e.target).is('.active')) {
      close_accordion_section();
    } else {
      close_accordion_section();

      // Add active class to section title
      jQuery(this).addClass('active');
      // Open up the hidden content panel
      jQuery('.nav ' + currentAttrValue).slideDown(300).addClass('open');
    }

    e.preventDefault();
  });
});
.nav {

  right: 0px;

  left: 0px;

  margin-top: 0px;

  margin-bottom: 20px;

  height: 35px;

  text-align: center;

  background-color: red;

  z-index: 1;

}

.sticky {

  background: #000;

  text-align: center;

  margin: 0 auto;

  position: fixed;

  z-index: 9999;

  border-top: 0;

  top: 0px;

  font-size: 17px;

  box-shadow: 0px 0px 3px 0px #949494;

  background: #10b5fa;

  padding-top: 6px;

}

.accordion-section-title {

  width: 100%;

  padding: 10px;

  display: inline-block;

  border-bottom: 1px solid #1a1a1a;

  background: #333;

  transition: all linear 0.15s;

  /* Type */

  font-size: 1.200em;

  text-shadow: 0px 1px 0px #1a1a1a;

  color: #fff;

  text-decoration: none;

}

.accordion-section-title.active,

.accordion-section-title:hover {

  background: #4c4c4c;

  /* Type */

  text-decoration: none;

}

/*----- Section Content -----*/

.accordion-section-content {

  padding: 10px;

  display: none;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<nav class="nav sticky">
  <div class="accordion-section"><a class="accordion-section-title" href="#tujuan"> Kategori</a>

    <div id="tujuan" class="accordion-section-content">
      <p>Hello World</p>
    </div>
  </div>
</nav>

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 are the steps for adding a PDF attachment to an email using nodemailer?

I want to send email with attachments dynamically using Nodemailer and jQuery in the front end. <script> $(document).ready(function () { var from, to, subject, text; $("#send_email").click(function () { to = $("#to").val(); s ...

Node.js and Socket.IO: Managing responses

In a unique scenario, the web page is initially served using HTTP. When the submit button is clicked, data is sent to the server and multiple web services are executed, which may take some time. The challenge is to quickly display the response page and the ...

Creating a CSS animation to repeat at regular intervals of time

Currently, I am animating an SVG element like this: .r1 { transform-box: fill-box; transform-origin: 50% 50%; animation-name: simpleRotation,xRotation; animation-delay: 0s, 2s; animation-duration: 2s; animation-iterat ...

Tips for avoiding background color interference with raycaster

In my current three js scene, I have a ground, sky, and various objects. I want specific objects to change color to red when the mouse hovers over them, but not all objects should do this. Currently, everything I touch turns red, which is not what I want. ...

If you're not utilizing v-model.lazy, Vue3 Cleave js may encounter functionality issues

I am currently experimenting with cleavejs to format the thousand separator in my input numbers. I've noticed a strange behavior where if I input 1000.123, it displays as 1,000.12 which is the correct format. However, the v-model value remains as 1000 ...

Looking for assistance with transferring API information to components

I am currently working on a basic Movie finder application that will display a list of movies containing the name entered by the user. My focus at this stage is to just show the information on the screen. As I'm using React for this project, I have t ...

Styling columns to be inline-flex without taking up the full height

I'm currently working on a project similar to "Trello" or "Zenhub," using a Kanban board with 4 columns placed in a div#board. The columns are set to display: inline flex, creating a neat horizontal alignment. However, I encountered an issue where eac ...

Change the background image when hovering over an element with vanilla JavaScript by utilizing data attributes

I would like to be able to change the background image of a <div> when hovering over a link. Initially, the <div> should not have a background image when the page loads for the first time. This is my current setup: <div class="background"& ...

Enhancing collapsible list headers in jquery mobile with checkboxes

Having trouble with a jQuery Mobile website issue. Currently working on a jQuery mobile site that includes a collapsible list (). The client request is to have a checkbox inside the header, allowing users to check items off without needing to open them. T ...

Ways to forward from an ajax-triggered action

I am looking to immediately redirect from the "Login" action to the "Home" action without going through the ajax success callback. Currently, when the "Login" action is triggered via ajax, it redirects to the "Home" action, but the ajax success callback st ...

Vue alert: Issue with rendering - TypeError: Unable to access property 'NomeStr' as it is undefined

I'm having trouble displaying the 'NameSrt' item array value in my template, and I keep encountering this issue: vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in render: "TypeError: Cannot read property 'NomeStr' of undefined" The ...

Dynamic JavaScript tool

Does anyone know which library is being used on the website linked here? I am working on a project similar to this and would appreciate if anyone can identify this library for me. Thank you in advance. ...

transform a zipped file stream into a physical file stored on the disk

I have a Node application named MiddleOne that communicates with another Node App called BiServer. BiServer has only one route set up like this: app.get("/", (req, res) => { const file = `./zipFiles.zip`; return res.download(file); }); When Middl ...

Prevent postback in case of validation error

In my current setup, I have a textbox control with the following specifications: <asp:TextBox ID="txtAmount" runat="server" AutoPostBack="true" Width="85px" class="validate[ERExpenseTypeDaterequired,custom[float]]" On ...

Utilizing a Frozen Tensorflow Model with NodeJS for High-Performance Computing

I am new to tensorflowjs and js in general, but I have a trained model that I need to run on it. I have converted the model to json format, but I am having trouble feeding data into it: const tf = require('@tensorflow/tfjs') const tfn = require( ...

Fixed position does not adjust based on the screen resolution

If I were to create a web page, I would include a main menu that always remains on the screen, similar to this design: There are no issues with screen resolution exceeding 1300px, as seen in the image above. However, when attempting to resize the browser ...

Delay in only a portion of the AJAX response

I created a chat application that is functioning correctly, but I am encountering an unexpected behavior. When a user sends a message, the user's name, time, and message should be displayed in order. However, currently, it seems like the username and ...

Uploading files with Angular and NodeJS

I am attempting to achieve the following: When a client submits a form, they include their CV AngularJS sends all the form data (including CV) to the Node server Node then saves the CV on the server However, I am encountering difficulties with this proc ...

The webpage is failing to refresh even after using history.push()

While working on my React dictionary project, I encountered an issue with React Router. After using history.push() with the useHistory hook from react-router, the page does not re-render as expected. I have a search bar and utilize this function to navigat ...

NextJS does not support the rendering of the map function

Currently, I am getting acquainted with NextJS by creating a basic blog. I have successfully passed the data through props and can see it logged in the console within the map function. However, I am facing an issue where the HTML content does not display i ...