The has class method is failing to work within the if statement

I have encountered an issue with my Accordion code. When I test it, the word 'not' is displayed twice, while 'hve' is not displayed at all.

$('.st-accordion li a').click(function() {

  if ($(this).parent().not('activeaccordion')) {
    $(this).parent().addClass('activeaccordion')
    alert('not');
    $(this).next().slideToggle();
  } else {
    if ($(this).parent().hasClass('activeaccordion')) {
      alert('hve');
      $(this).parent().removeClass('activeaccordion')
      $(this).next().slideToggle();
    }
  }
});

Answer №1

Employ

if(!$(this).parent().hasClass('activeaccordion')){

Another option is to utilize

$(this).parent().toggleClass('activeaccordion');
to toggle the class on or off accordingly.

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

JSON error: Unable to access property 'xxx' as it is not defined

One way I extract an attribute value is like this: item['@attr']['nowplaying'] While this method is effective when the attribute exists within the json, it throws an error if the attribute is missing: Uncaught TypeError: Cannot read ...

After running JavaScript, retrieve the canvas data using the toDataUrl() method

After working on a Javascript function that creates a canvas and writes an image and text on it, I was expecting to get the base64 dataUrl. However, all I end up with is a blank canvas. Check out the code snippet below: function createBreadCrumb(label) ...

Exploring a JavaScript object to verify if a property contains nested data elements

I am currently working on traversing through the object above in order to determine if a contact is a member of a specific list. For instance, if the user is a member of the list with an ID of 2022, I want to display their first name (which is also includ ...

The colorbox popup is experiencing difficulty loading the Google Map from an inline div specifically in IE7

Using the following code, you can load a Google map from an inline div to a Colorbox popup window in all modern browsers. However, it may not work with outdated browsers like IE7. <head> <script src="http://maps.googleapis.com/maps/api/js?key=/// ...

The JQuery .click event is not functioning properly following the implementation of an AJAX filter

I am encountering an issue with the function below. The function is designed to toggle a div's visibility when clicking on a link with the "show-more" class. It also successfully changes an icon. However, the problem arises when I use an AJAX filter o ...

Extract ID for Bootstrap modal display

In my project, I am using a bootstrap modal that displays various strings. The challenge I am facing involves a loop of cards, each with a distinct 'id'. When triggering the modal, I want to show the corresponding id inside the modal itself, whic ...

How can I use JavaScript to add a search text to the selectpicker searchbox in Bootstrap, and then automatically display the search results in the dropdown menu?

In the process of developing a web application with Razor Pages ASP.NET CORE 3.0, I have integrated a Bootstrap "selectpicker" dropdown with the attribute "data-live-search= true". The dropdown is populated with the names of employees from my model. Below ...

What is the best way to link my PHP variable to a JavaScript variable within a While loop?

My goal is to create a website that retrieves information from a MySQL database and displays pictures upon clicking a button. echo "<img align=\"left\"src=\"".$path[$y]."\" alt=\"error\">"; echo "<img align=\"r ...

Get the nearest offspring of the guardian

I have a main 'container' div with multiple subsections. Each subsection contains 1 or 2 sub-subsections. Let's say I have a variable that holds one of the subsections, specifically the 4th subsection. This is the structure:container > s ...

Transform C# ASPX to handlebars format

Initially, my task was to choose and relocate a single li from many options into a different div. But now, I am required to achieve the same result using a JS template which utilizes handlebars. The C# section is as follows: <li class="l-row__item pr ...

How can I make the background of a button change when I move my cursor over it

Is it possible to change the background image of a button when hovering over it? Perhaps have the image transition from left to right for a fading effect? Can this be accomplished? ...

Adjust the size of a div element after updating its content using Mootools

I am currently utilizing mootools 1.2 on a basic webpage. There is a div that contains a form (referred to as formdiv) which is submitted through JS/ajax. Once the ajax request returns a "confirmation message" data, the formdiv updates accordingly. The is ...

Ensure that the content is perfectly aligned with the rest of the page for a visually pleasing

Seeking guidance on how to symmetrically center an image gallery with a list background, regardless of the browser's size. Any tips for aligning content perfectly? I attempted using a wrapper around the gallery and list with a fixed width, but it onl ...

Ways to generate a unique link for every value in an option tag dynamically?

I'm currently working on a form that allows customers to buy an online service. Within the form, there's a dropdown list featuring 'select' and 'option' tags. This dropdown menu lets users pick a billing cycle (6 months, 1 ...

interactive web elements powered by html and javascript

When utilizing ajax, I am able to dynamically generate html code in response to a button click. The html code is designed to link various figures together. Here is an example of the html code: <a class="show_figures" id="show_figures0" loop-id="0" hre ...

Retrieve the added attribute value of a RadioButton using JQuery

Currently, I am faced with a frustrating issue. On my website, I have a RadioButton to which I added an attribute as shown below: in ...asp <div id="radio" class="C_Radio"> <asp:RadioButton id="rad1" GroupName="typeSelect" runat="server" ...

confirm chosen option html php

Is there a way to validate the select tag so that an alert appears when the user tries to submit without selecting a session? Below is the HTML and PHP code I have been using, any help would be appreciated. <select name="ses"> <o ...

How come I am unable to fetch JSON data using JavaScript from PHP?

My goal is to upload a file and extract some processed data from it using PHP. However, I have encountered some issues. Below are my simplified code snippets. HTML CODE <form action="ajax.php" method="post" enctype="multipart/form-data"> <input ...

The JSX element 'body' appears to be missing its closing tag

I'm currently in the process of developing a landing page using react.js. This particular page is designed for users who are not yet signed up to create an account if they wish to do so. Unfortunately, I'm encountering some errors, one of which p ...

Extract information from a webpage containing various tabs through web scraping

My goal is to extract information from this specific page. Upon loading, the page defaults to the first Tab. I have successfully scraped data from this initial tab without issues. However, I now need assistance in navigating programmatically to the second ...