What causes parent class to clear input?

I am attempting to toggle a class on a parent element (either a div or form). My CSS adjusts the visibility of certain form elements based on this parent class.

However, when the class is changed on the div or form, all the inputs within the form are cleared or set to no value. While this functionality works for most of the inputs, I do not want the values to be lost for those that are always visible even when hiding the form.

In essence, I have a basic search form and an advanced one.

UPDATE: Vanilla JavaScript (not jQuery) behaves as expected... so why jQuery?

var d = document.getElementById("div");
d.className = d.className + " advanced";

Here is the complete fiddle: https://jsfiddle.net/psalmody/Lsnsaups/2/

CSS:

#div .search-advanced {
  display:none;
}

#div.advanced .search-advanced {
  display:inline-block;
}

HTML:

<div id="div">
<form id="searchForm">
<fieldset>
  <div>
    <label for="term">Semester:</label>
    <select id="term">
      <option disabled></option>
    </select>
  </div>
  <div>
    <label for="subj">Subject(s):</label>
    <input type="text" id="subj"></select>
  </div>
  <div class="search-advanced">
    <label for="crse">Course Number:</label>
    <input type="text" id="crse" maxlength="5">
  </div>
  <div class="search-advanced">
    <label for="sec">Section:</label>
    <input type="text" id="sec" maxlength="3">
  </div>
  <div class="search-advanced">
    <label for="crn">CRN(s):</label>
    <input type="text" id="crn">
  </div>
  <div class="search-advanced">
    <label for="title">Title:</label>
    <input type="text" id="title">
  </div>
</fieldset>
</form>
</div>

jQuery:

$(function () {
    $('#advanced').click(function () {
        $('#div').toggleClass('advanced')
    })
})

Answer №1

When you utilize the <button> element with a type of reset, it will restore all the form's controls to their original values. As stated in the HTML specification:

A button represented by the input element that, when triggered, resets the form.

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

Node.js now supports ES6 imports using the `--experimental-modules` flag,

Experimenting with ES6 imports in node using the -experimental-modules flag. Here are the steps: mkdir testfolder cd testfolder npm init npm i --save testing-library touch script.mjs Next, add the following code to script.mjs: import { test1, test2, tes ...

Displaying a loading GIF during a standard form submission

I am facing a challenge with a form in my concept, as it takes at least 10-15 seconds to load due to a large number of records. While I know I could use AJAX to display a loading GIF and load the records simultaneously, I am wondering if there is a way t ...

jQuery show/hide function malfunctioning

Currently, I am attempting to create a basic show/hide interaction for a div using jQuery. The goal is to make the artists-home div vanish and be replaced by the .ken-gallery when the .artist-ken is clicked. Here is my current code, although it is not fun ...

Trigger functions when the window is scrolled, resized, or when the document is fully loaded

I have a few functions that need to be executed under specific conditions: window.scroll window.resize document.ready For example: <script> function myFunction1(data){ /*code*/ } function myFunction2(data){ /*code*/ } ...

When using classList.replace, it should swap out every instance of the class xxx1yyy with xx

I attempted to swap a class and came across this helpful example here: javascript: replace classList that is inside a conditional Unfortunately, my attempt at modification (shown below) did not work. The use of classList.replace should change all instanc ...

jQuery Update for Header/Footer Navigation

I have implemented header and footer navigation with different states using jQuery. However, I am encountering an issue when trying to update the header nav upon clicking on the footer nav items. Specifically, I want the header nav to reflect the same sele ...

What is the best way to merge two ajax functions into a single function?

I am looking to streamline my ajax functionality by combining two separate functions. One function is used to post a name, while the other is used to upload an image. I would like to create a single function that can handle both tasks of posting a name and ...

Python Django Notification SystemIncorporating real-time

As a newcomer to web application development, I am currently utilizing Django in one of my projects. Within this project, I have an upload page where I aim to incorporate JavaScript/jQuery notifications. Specifically, after a successful upload, a brief mes ...

Guide on making an API call using the map function

Hey there! I'm currently trying to make an API call within a map function, but it keeps returning as an object.promise. Is there a way to call the API within it without getting this object.promise result? const MatchHistory = ({ history }) => { ...

Execute a multer request to upload an image (using javascript and express)

I am utilizing the Express server as an API gateway for my microservices, specifically to process and store images in a database. The issue at hand is that within the Express server, I need to forward an image received through a POST request from the clien ...

I'm seeking assistance with a frontend script problem. I'm curious if there are alternative approaches to coding this script that may be more effective. Can anyone offer guidance on this?

As a frontend developer specializing in script injection, I have been utilizing Adobe Target to inject scripts. However, this method presents several challenges: 1. It is difficult to debug code errors as my HTML and CSS are wrapped inside ' ' a ...

Utilizing WordPress Variables Beyond the Loop

Following the update of my loop to gather all necessary data using this code: <?php $json = ""; if ( have_posts() ) { while ( have_posts() ) { the_post(); global $wpdb; $query = "SELECT * FROM ". $wpdb-&g ...

Unable to transmit a variety of text or variables via email through PHP

Can someone assist me in figuring out how to send more content in an email? I have been attempting to include additional information in the email, but haven't been successful. I am using ajax to call and send the email, however, only basic emails seem ...

How can I annihilate the reveal effect in Foundation 6?

I've encountered a problem with initializing reveal in Foundation 6. Specifically, I'm attempting to create a new instance for #menu on medium and small screens and remove it on large screens. However, I'm running into issues removing the da ...

Access parent component's properties by clicking on child component

I'm uncertain if my current approach is the most effective solution to my issue, so I am open to alternative methods. My goal is to access the properties of the parent component when clicking on a child component (Image). Below is the code I have imp ...

Continuously making ajax requests as each one finishes

I am facing a situation where I have multiple ajax calls that need to be executed one after the other in a sequence. Initially, chaining them together manually worked fine with a few calls. However, now that there are around 20 ajax calls, it has become me ...

Custom providers do not override Angular UrlResolver

In my Angular application, I am trying to implement a custom UrlResolver provider to incorporate cache breaking logic. I came across this question on Stack Overflow: . Unfortunately, it seems that overriding the default compiler UrlResolver using a provid ...

Is there a way to eliminate this pesky margin?

please check out this: https://jsfiddle.net/desytec/73qdtejg/1/#&togetherjs=w3lvLQi0v6 This displays the following table definition: <table id="semana" class="table table-striped dt-responsive table-bordered display ...

The <SelectField> component in material-ui is not displaying items properly in ReactJS

I am facing an issue with Material-UI where the items are not showing up. I am trying to display categories of products in a SelectField but it doesn't seem to be working. When I click on the SelectField, no items are displayed. Below is the code for ...

If both the p and blockquote elements are present, encase them in a single box

I have the following HTML structure: <div class='header'>...</div> Depending on the file, this div might contain different elements. In some instances, it could look like this: <div class=' ...