Issue with toggleClass() function on a div element

I've been developing a website and encountered an issue. In my FAQ section, I have a drop-down menu along with a submit button that directs to different parts of the FAQ page. Everything seems to be working fine so far - the submit button triggers a javascript function that dynamically links to the relevant anchor on the page. However, I'd like the selected question in the dropdown menu to be highlighted orange. While I was able to achieve the highlight effect briefly, it disappears quickly after appearing. I'm unsure why this is happening and how I can make the highlight stay without flashing away. Additionally, I'm interested in making the highlight fade in using jQuery. I know that hiding the div then using .fade() can do the trick, but it results in all text disappearing. Any help or suggestions would be greatly appreciated!

Here's a link to a fiddle for reference: http://jsfiddle.net/UjPtv/. It doesn't work completely because when you submit, it displays an error. My assumption is that jsfiddle might not allow submits even though this submit simply runs some JavaScript code. If you have a workaround for this issue, please share it with me so I can update the link. Thank you!

Javascript:

var toggled = 0;
function jump_to (location) {
if(toggled == 0){
    toggled = 1;
    window.location.href = "#" + location;
    $("#wrap"+location).toggleClass("wraps_active");
}
}​

HTML:

<div id = "main">
        <h2 class = "center">FACTS YOU NEED</h2>
        <h3>The Core Facts You Want To Know</h3>
        <div class = "border">
        <p>
            Jump To: 
              <form onsubmit = "jump_to(jump_list.value)">
              <select name = "jump_list">
              <option value="1">Who can go to the camp?</option>
              <option value="2"><a href = "contact_information.html">When do we check in and out?</a></option>
            </select>
            <input type = "submit" value = "Go"/>
            </form>
        </p>
        </div>
        <div id = "wrap1" class = "wraps">
        <h4><a name = "1"></a>Who can go to the camp? </h4>
        <p>
            The camp is for kids ages 9 to 17 years old who have been diagnosed with celiac disease, a condition that requires life-long adherence to a gluten-free diet.  Given limited space, we are unable to accommodate kids on gluten-free diets for other reasons (such as autism spectrum disorders).  Campers ages 16-17 years may choose to volunteer as junior counselors.  Junior counselors assist the adult volunteers, and for first session junior counselors need to arrive a day early (on Monday) for the staff orientation and training.  If there are more junior counselor applicants than there is available space, priority is given based on age, gender-need, and prior camp experience.
        </p>
        </div>
        <div id = "wrap2" class = "wraps">
        <h4><a name = "2"></a>When do we check in and out?</h4>
        <p>
            Check-in: Please see the "Calendar" on the left hand side of the website for details about each camp.Please do not arrive early.
            </br><br/>
            Check-out:  All campers must be checked out by the stated time so camp can be prepared for the next group, see the "Calendar" for date information.
        </p>
        </div>


    </div>​

CSS:

.wraps{
border-bottom:1px dashed #C3C3C3;
}
.wraps_active{
border-bottom:1px dashed #C3C3C3;
background:#FFB280;
}​

Answer №1

Whenever the form is submitted, remove all existing active wrap classes and only add it for the selected one.

$('form input[type=submit]').click(function(event) {
    event.preventDefault();

    var location = $('select[name=jump_list]').val();
    window.location.href = "#" + location;

    $('.wraps').removeClass('wraps_active');
    $("#wrap"+location).addClass("wraps_active");
});

Check out this Fiddle.

Answer №2

Give this a shot:

$("#container"+position).toggleClass("active_container");

Swap out addClass for better results.

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

PHP data grid selects a field and dynamically generates a dropdown menu from other tables

I'm currently using phpMyDataGrid and I'm having trouble implementing a dropdown list menu in one of the columns. $objGrid = new datagrid; $objGrid->closeTags(true); $objGrid->friendlyHTML(); $objGrid->methodForm("get"); $objGr ...

Error: Material-UI prop type validation failed - Please specify either `children`, `image`, `src`, or `component` prop

Despite having an image prop in CardMedia, I kept encountering this error. Here is a snippet of the code: Error description: const Post = ({ post, setter }) => { const classes = useStyles(); return( <Card className={classes.card} ...

Separate the label and content sections in an Angular Material vertical stepper

https://i.sstatic.net/S1gIH.jpg After applying the following CSS: mat-step-header{ display: flex ; justify-content: flex-end ; } I am attempting to make this stepper function properly. I have implemented Angular Material design for a vertical stepp ...

Storing checkbox status in Angular 7 with local storage

I am looking for a way to keep checkboxes checked even after the page is refreshed. My current approach involves storing the checked values in local storage, but I am unsure of how to maintain the checkbox status in angular 7 .html <div *ngFor="let i ...

Does Google Cloud CDN not store data from the storage bucket?

I've been experimenting with combining a Load Balancer, Cloud Storage, and CDN for the past few weeks. Unfortunately, I have not been successful in getting it to work as expected. I uploaded some static files (2 jpgs, svgs, and css files) to a multi- ...

Tips for Incorporating xmlhttp.responseText in If Statements

This is the code snippet from my save_custLog_data.php file: <?php $a = $_GET['custEmail']; $b = $_GET['pswrd']; $file = '/home/students/accounts/s2090031/hit3324/www/data/customer.xml'; if(file_exists($fi ...

What does it mean in Javascript when b1 is undefined while o1 has a value and is equal to b1?

Having some issues getting variables to work with drop down options on a page. At first, I couldn't even extract a value from the function but managed to do so by removing "var" from o1. Strange thing is, when I type o1 into the js console on chrome i ...

Tips for eliminating the jittery motion in an image gallery transition

I have a gallery of images that seems to be quite 'jumpy' when opening and closing the images, as demonstrated in this fiddle. Even after applying transform:translateZ(0); as suggested here, there doesn't seem to be much improvement in redu ...

Unable to successfully submit a form using PHP

I am currently working on a PHP page that retrieves data from MySQL. The goal is to fetch a list of objects from the database, display each object in a separate form for editing, and then save only the edited values. However, I am encountering difficulties ...

How can I alter the background color while performing a transformation in JS/CSS?

I'm currently working on achieving a flipboard effect for some divs, where one side is white and the other is black. Here's what I have so far: setInterval(function () { document.getElementById('test').classList.toggle('flipped& ...

Learn the steps to show a checkbox in a Kendo Grid column

Having an issue displaying a checkbox in my grid due to an error I keep encountering. The template for my checkbox is as follows:{ field: "IsForeign", title: "Is <br/> Foreign", ...

Tips for removing markers from personal Google Maps

I am having trouble deleting markers from my Google Maps using my code. The markers array seems to be empty even after adding markers. Can anyone help me troubleshoot this? Thank you! When I use console.log(markers.length) in the removeMarkers() function, ...

Obtaining the real file from the input with jQuery AJAX for uploading

Can someone help me with a file upload to Flask server using jQuery AJAX? My jQuery skills are lacking, and I'm struggling to fetch the file from the input field for my AJAX call. Here is the HTML: <form name="csvimportdataform" id=" ...

Understanding the concept of nested menus in ASP.NET

A table displaying a list of items with their corresponding details is shown below: ItemID MENU ParentID ImgUrl ----------- -------------------- ----------- ----------- 1 Home 0 ...

Is there a way to apply a class to the main div once the checkbox has been clicked?

Whenever the checkbox is checked, it should add a class to the main div. Additionally, if I try to check another checkbox, the previously checked one should have its class removed and the new one should have the class added. Here is the code snippet for r ...

Implementing styling and adding a tooltipster to a select2 element that is created dynamically

In a partially dynamically created page, select2 elements are returned from an ajax call. jQuery manipulation is necessary in order to interact with the forms displayed in the complex table structure. The goal is to add a red border and a tooltipster when ...

Align the text in the center of the image, which is depicted as a bullet point

I am currently using images as bullet points for my Github Pages (markdown files), following the instructions outlined here. My goal now is to vertically center the text next to the image. This is demonstrated here (cf. lower third of page), however, I am ...

I'm attempting to switch between different "screens" by utilizing divs with CSS and JavaScript, using the display property to toggle between none and block. Everything seems to be functioning smoothly except for the

I thought I grasped the concept of this because all my toggles are working fine, except for the last one. The frustrating part is that it's written in the same manner as the others before it. The problematic button is identified by id="startYearOne". ...

Using jQuery and Bootstrap to Enhance Checkbox Button Styling

I am currently working on an asp.net webform and attempting to style my checkbox similar to the design shown here: When I try to replicate the HTML and JS code from the above link on my page, it does not seem to be functioning properly, most likely due to ...

Sending an Ajax request to the MVC Controller results in a response of "NOT FOUND"

Trying to use AJAX to call a MVC Controller action with certain parameters has been successful in the past within this application. However, for some reason, THIS SPECIFIC ONE is not functioning properly. function deleteDetail(button, tab) { var i ...