Seems like JavaScript's .active functionality is failing to function properly in my case

I'm encountering an issue with my website's homepage. I have a list of services displayed, and the intention is for a description to appear when one of the services is clicked. However, clicking on the buttons does not trigger the expected action, and I'm unsure of the reason behind this. The JavaScript function used for this functionality is similar to what I've successfully implemented for the menu button.

I don't consider myself proficient in coding and still classify myself as a beginner. Any assistance regarding this matter would be greatly appreciated!

$(document).ready(function() {
  $('.video-btn').click(function() {
    $('description-video').toggleClass('active');
  })
  $('.animation-btn').click(function() {
    $('description-animation').toggleClass('active');
  })
})
.description {
  width: 600px;
  font-size: 1.4em;
  line-height: 1.2em;
  font-weight: 400;
  color: #f4f4f4;
  padding-left: 1em;
  border-left: 4px solid #e0bd8c;
}

#description-video,
#description-animation {
  display: none;
}

#description-video.active,
#description-animation.active {
  display: inherit;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="service-list">
  <li id="video-btn"><a href="#">Video</a></li>
  <li id="animation-btn"><a href="#">Animation</a></li>
</ul>

<p class="description" id="description-video">
  Text goes here...
</p>

<p class="description" id="description-animation">
  Text goes here...
</p>

Answer №1

There are a couple of things to note:

In the click identifiers, they are referred to as classes, but in your HTML you have defined them as ids.

  $('#video-btn').click(function() {
  $('#animation-btn').click(function() {

You have forgotten to add the prefix on the toggleClass identifiers.

$('#description-video').toggleClass('active');
$('#description-animation').toggleClass('active');

Answer №2

  • .video-btn represents a class. Ensure you have the appropriate IDs
  • Although you have links, the event handler is actually on the LI element
  • Make sure to include a # in the selector

To optimize your code, consider delegating and utilizing data-attributes

$(function() {
  $('.service-list').on("click",'.btn a', function() {
    $("#"+this.dataset.desc).toggleClass('active');
  });
});
.description {
  width: 600px;
  font-size: 1.4em;
  line-height: 1.2em;
  font-weight: 400;
  color: #f4f4f4;
  padding-left: 1em;
  border-left: 4px solid #e0bd8c;
}

#description-video,
#description-animation {
  display: none;
}

#description-video.active,
#description-animation.active {
  display: inherit;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="service-list">
  <li class="btn"><a href="#" data-desc="description-video">Video</a></li>
  <li class="btn"><a href="#" data-desc="description-animation">Animation</a></li>
</ul>

<p class="description" id="description-video">
  Text goes here...
</p>

<p class="description" id="description-animation">
  Text goes here...
</p>

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

No modifications to the CSS can be made within the "alterations" section of the Console drawer in Chrome

Is there a way to easily track and summarize all of my live CSS changes in the Chrome browser? I've searched on Stack Overflow, but haven't found a solution that works for me. This specific answer seems like it could be the most helpful for achie ...

Embed fashion and graph elements into a CSV document (generated from <script>)

Encountering an issue with my code. I am looking to export an HTML table to a CSV file (specifically Libre Office Calc, regardless of csv, xls or xlsx format, as long as it runs on a Linux Server). Found a script online that works well after some modificat ...

Setting up the current user's location when loading a map with Angular-google-maps

I am currently utilizing the library in conjunction with the IONIC framework. To manually set the center of the map, I have implemented the following code snippet: .controller('mainCtrl', function($scope) { $scope.map = { cen ...

CSS code to create a single content bar flanked by two sidebars

I have been working on a beginner-friendly webpage and have managed to style it using CSS to some extent. My goal is to have the content centered with a white background, flanked by two sidebars styled in blue. To work on this page, please visit: The ...

Using VueJS to showcase user input in a dynamic list and a pop-up modal

I am attempting to achieve the following: Use a v-for loop to display form input (name, position, company) as an unordered list, showing only the name input and a button for each person When a button is clicked, a modal will appear displaying all the data ...

Analyzing conditional statements in React with State management

I've been encountering challenges with if statements correctly evaluating, displaying either true all the time or exhibiting unexpected behavior when passing variables or using state variables for evaluation. I realize this might be related to differe ...

Prevent users from changing dropdown options after a form submission fails using Javascript and Jquery

Currently, I am pursuing the tech degree at Team Treehouse and my third project involves creating an interactive form. To simplify my issue, I will outline it in bullet points: The credit card payment method should be preselected when the page loads. I n ...

Image fails to download on Safari and iPhones in PHP

Can anyone help me figure out why images are opening in a new window instead of downloading inside Chrome and Firefox on my iPhone? ...

Resource loading unsuccessful: server encountered a status of 500 (Internal Server Error)

I'm struggling to figure out why I keep getting an Internal Server Error when trying to call a web service in my HTML page using JavaScript and Ajax. Here is the error message: Failed to load resource: the server responded with a status of 500 (Int ...

What is the best way to organize an object that includes both numbers and strings?

Presently, I have an array shaped like this (for example): const items = [{ name: 'LFB 1Y GF'}, {name: 'LFB 3M GF'}, {name: 'LFB 2Y GF'}, {name: 'LFB 10Y GF'}, {name: 'LFB 5Y GF'}] The desired ordering of ...

Is it necessary to test the main.js file in Vue.js project?

While performing unit tests on components is acceptable, after running coverage analysis I have noticed that certain lines of code in the main.js file remain uncovered. This raises the question: should the main.js file also be included in testing? Specifi ...

Exploring the power of $j in JavaScript

Could someone please explain what the $J signifies in this snippet of javascript code? if ($J('.searchView.federatedView.hidden').attr('style') === 'display: block;' || $J('.searchView.federatedView.shown').length ...

Adjust the text color of ASP.Net Label based on the presence of a hyphen

I need help changing the text and font color of my ASP.net label based on its content. The label displays a percentage change, and I want negative numbers to be shown in green and positive numbers in red. However, when I try to implement this, I keep enc ...

Ways to identify an element within a webpage

I'm currently working on creating a chrome extension that can detect the presence of specific elements on a webpage. The goal is to have this extension automatically run every time a new page is opened and display a popup message if the element is fou ...

How can I modify the border color of a Material Ui TextField component?

I've been struggling to figure out how to customize the color of the TextField border. I want to change it from the default grey to black. Below is a simplified version of my UpdatePage.js file: const useStyles = makeStyles(() => ({ updatePage ...

Is it possible to prevent the selected option from being available in other select lists using AngularJS?

Can anyone help me figure out how to disable an option in one select list when it is selected in another using AngularJS? I have set up a select list for From Year and To Year with ng-repeat, which is working fine. Now, I just need to implement the functio ...

The issue with Three.Js Raycasting arises when attempting to change camera transformations within an Object3D parent element

In my latest project, I decided to create a "camera controller" that handles the movement and rotation of the camera by utilizing an Object3D as its parent. The Y-axis rotations are applied to the Object3D, while the X-axis rotation is directly applied to ...

What does the typeof keyword return when used with a variable in Typescript?

In TypeScript, a class can be defined as shown below: class Sup { static member: any; static log() { console.log('sup'); } } If you write the following code: let x = Sup; Why does the type of x show up as typeof Sup (hig ...

Utilizing jQuery and AJAX to dynamically include multiple PHP pages

My webpage contains a div tag similar to this structure: <div class="ajax-load"> <!-- First.php: jquery-slider-1 jquery-widget-1 second.php: jquery-slider-2 jquery-widget-2 --> </div> There is also a ...

Retrieve the latest information and update the database with just one ajax request

I am attempting to update a row in the database and retrieve the updated row one at a time using an AJAX call. JavaScript inside the ready function $("button[name='teacher_lock_exam']").on(ace.click_event, function () { var current_exams_id ...