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

Issue encountered while trying to play sound on PhoneGap

Greetings! I am attempting to integrate sound into various buttons and components of a game. I found this code on the PhoneGap website which works perfectly fine during testing: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "h ...

"Initiate an Ajax call in Full Calendar before an event is displayed on the calendar

I need guidance on how to integrate ajax calls with the Full Calendar documentation. Specifically, I want to make an ajax call to a WordPress database before each event is rendered on the calendar. The response from the call will determine the color of the ...

Adding JavaScript to dynamically loaded AJAX content

I'm new to AJAX and JavaScript and unsure of how to get it working. Here is the website: When you click on portfolio images, the details load via AJAX. I want to create a slideshow for projects with multiple full-sized images. However, due to the co ...

Using the ng-show directive in AngularJS allows you to pass elements in

Web Development <li ng-show="sample($event)" TestLi</li> JavaScript Functionality $scope.sample = function($event){ //$event is undefined //perform some action } I have experimented with passing the HTML element using ng-click, but I am curio ...

Enhance Your Website with Interactive Tooltips Using Twitter Bootstrap

In Twitter bootstrap, the default trigger for tooltips is hover. If I want to make the tooltip display on focus instead, I can add data-trigger="focus". But how do I make it so the tooltip displays both on hover and on focus? ...

Invoking a React function repeatedly (every second)

Currently, I am working with React and utilizing Material UI to create a modal. The modal is rendered as part of the body of the code, placed at the bottom of the page. Its visibility is controlled by the state; if it's open or closed. However, I&apos ...

Show a Toast in React without relying on useEffect to manage the state

I have successfully implemented the Toast functionality from react-bootstrap into my application using the provided code. However, I am unsure if it is necessary to utilize useEffect to set show with setShow(items.length > 0);. Would it be simpler to ...

Exploring the functionalities of can-deactivate without incorporating routing

I'm facing an issue with a parent component and multiple child components. The parent component contains a form, while each child component also has its own form. Unfortunately, all child components share the same route as the parent component and hav ...

Problem encountered while downloading dependencies with Snyk

While attempting to set up the dependencies for the W3C Respec project, I encountered this error message: npm WARN prepublish-on-install As of npm@5, `prepublish` scripts are deprecated. npm WARN prepublish-on-install Use `prepare` for build steps and `pr ...

Capturing errors from various pages with the Jquery Ajax .ajaxError event

Is there a way to display various Ajax request response errors in a universal dialog, similar to a standard error page? Can we capture error descriptions from multiple Ajax calls on different pages and display them using a common Ajax error event? $(doc ...

Using jQuery in CodeIgniter to create a dynamic dropdown list that is dependent

After successfully implementing a working dropdown dependent form, I encountered an issue where hidden input fields within the hidden div were still submitting post values. Is there a way to prevent hidden inputs from being submitted when their parent div ...

Encountering [object, Object] while attempting to display JSON object retrieved from req.body in the console

While attempting to insert a product's details into my API's PostgreSQL database using Postman, I encountered an issue where the values of the specs key were displayed as [object Object] instead of showing the complete data. As a result, even in ...

Angular is sending a parameter with a null value, which is not supposed to happen

My filtering system used to work well with a code that displayed items of specific status. However, I had to modify it to match a select input requirement. <ul id="dropdown-basic" *dropdownMenu class="dropdown-menu" role="menu" aria-labelledby="button- ...

Styled-Component: Incorporating Variables into Styled-Component is my goal

Currently, I am working on an app and have created a separate file for styling. I decided to use style-components for custom CSS, but faced an issue where I couldn't access variables instead of HEX values. Even after storing color values in a variable ...

Exploring Alternative Methods for Serving Static HTML Files in an Express Server

Hey there, quick question for you. Let's say I have two static HTML files that I want to serve in Express - 'index.html' and 'contact.html'. I've been playing around with some basic Express code to get them served: const expr ...

Storing chosen choices from various select lists with similar class name in JavaScript

I am struggling to save all the selected options in an array for future output. Despite trying various suggestions, I haven't been able to make it work yet. The class names and names cannot be modified. HTML <select class="addon addon-select" nam ...

What is the best way to fill in references that are nested within an array object using mongoose?

How do I go about populating the product fields within the cart array provided below? { "_id": "5bbd1c6e2c48f512fcd733b4", "cart": [ { "count": 1, "_id": "5bbd30ed9417363f345b15fc", "product": "5ba93a6d ...

Having trouble incorporating autocomplete search results into an HTML table using Jquery, Ajax, and JSP

My current project involves an App that utilizes AJAX with jQuery to communicate with a Spring-boot REST controller. While the app is functional, I am facing difficulty in displaying the search results neatly within HTML tables. result Here is a snippet ...

What could be causing the npm mysql module to malfunction when trying to initiate the 'connect()' function in a separate .js file?

When I call require('mysql') and use the function connect() everything works fine. However, if I try to call the 'connect()' function in another file, it throws an error saying connection.connect is not a function... Any suggestions on ...

CAUTION: Attempted to load angular multiple times while loading the page

I encountered a warning message while working on my project, causing errors in calling backend APIs due to duplicate calls. Despite attempting previously suggested solutions from the forum, I am stuck and seeking assistance. Can anyone provide guidance? Be ...