Remove the class attribute from the ancestor element using jQuery

Hey everyone, I'm facing an issue trying to remove the class attribute from the parent element of an input field. Here is the HTML snippet:

<div class="settings">
      <div class="settings-content">
          <ul class="general-settings">
            <li>
              <label>Name</label>
                <span class="hidden_elm">
                  <form class="name_form" id="myform" method="post" action="#">
                    <ul>
                     <li><label>First</label><input class="input-log" type="text" name="firstname" placeholder="First Name..."></li>
                     <li><label>Last</label><input class="input-log" type="text" name="lastname" placeholder="Last Name..."></li>
                     <li><hr></li>
                    </ul>
                    <input class="log-btn success-btn" type="submit" value="Save">
                    <input class="log-btn close_current" type="reset" value="Cancel">
                  </form>
                </span>
             </li>
          </ul>
        </div>
    </div>

Here's the jQuery code:

$(document).ready(function(){
    var hide_elm = $('span.hidden_elm');
    var clickon_elm = $('ul.general-settings > li');
    var ul_backgrounf = $('ul.general-settings');
    clickon_elm.on('click',function(){
        //remove the class if it exists
        $('.show_elm').removeClass('show_elm');
        $('.current_elm').removeClass('current_elm');
        //add the class attr to the current element
        $(this).addClass('current_elm');
        $(this).children('span').toggleClass('show_elm');
        //$(this).css({'height':'auto'});
    });
    $('.close_current').on('click',function(){
        //$(this).removeClass('current_elm');
        $('.current_elm').removeClass('current_elm');
        $(this).parent('span').removeClass('show_elm');
    });
})

CSS styles:

div.settings{
    margin-top: 10px;
}
ul.general-settings{
    width: 100%;
    /*padding-top:8px;*/ 
    padding-bottom:8px; 
    border-top: 1px solid #ccc;
    /*background-color: #f2f2f2;*/
    /*border-bottom: 1px solid #ccc;*/
}
ul.general-settings > li{
    width: auto;
    padding: 1px;
    margin: 0 auto;
    height: 30px;
    display: block;
    line-height: 1em;
    vertical-align: middle;
    border-bottom: 1px solid #ccc;
}
/** change style of the li element of the current show_elm **/
li.current_elm{
    background-color: #f2f2f2 !important;
    height: auto !important;
    cursor: default !important;
}
ul.general-settings > li:hover{
    cursor: pointer;
    background-color: #f2f2f2;
}
ul.general-settings > li > label{
    position: relative;
    float: left;
    width: 20%;
    text-align: left;
    padding: 10px 5px;
    color: #000;
    font-size: 1em;
    font-weight: 700;
}
ul.general-settings > li > label:hover{
    cursor: pointer;
}
/** show the hidden element **/
ul.general-settings > li > span.show_elm{
    margin: 0 auto;
    padding: 0;
    display: inline-block;
    width: 70%;
    padding: 5px 0;
}
span.hidden_elm{
    display: none;
}
span.hidden_elm > form{
    margin:0 auto;
    padding: 0;
    display: inline-block;
}
span.hidden_elm > form > input.log-btn{
    top: 0;
    right: 0;
    float: left;
    border: none;
    cursor: pointer;
    margin: 0 3px 0 0;
}
span.hidden_elm > form > ul{
    width: 100%;
}
span.hidden_elm > form > ul > li{
    display: block;
    margin-bottom: 5px;
    text-align: right;
    width: 388px;
}
span.hidden_elm > form > ul > li > label{
    padding-right: 5px;
}
hr{
    background: #d9d9d9;
    border-width: 0;
    color: #d9d9d9;
    height: 1px;
}

So, my goal is to eliminate the class:current_elm and class:show_elm when clicking on the cancel button which has a type of input:type=reset

Answer №1

Consider implementing the parents() method

     $('.close_current').on('click',function(){
        $(this).parents('.current_elm').removeClass('current_elm');
        $(this).parents('span').removeClass('show_elm');
    });

Answer №2

Based on my understanding of your request, you might want to consider the following solution:

$( "input:reset" ).on("click", function() {
  $(this).parents('span').removeClass("show_elm current_elm");
});

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

Vue 3 Option api: Issue with v-model input not propagating from child component to parent element

I am currently working on a new project using Nuxt 3, and I have encountered an issue with a contact form where the input values from a child component are not being received by the parent element. Let's dive into the code breakdown: Parent Component ...

Struggling to retrieve information from an XML file

As a newcomer to XML and JSON, I am facing difficulties in displaying my data in an HTML div. I am attempting to extract data from the xml api and showcase it within my div. Unfortunately, nothing is showing up in my div. Please excuse the messy code. If ...

Endless loading on NodeJS server local host

My NodeJS setup is serving files, but the page seems to be stuck loading. Here's a snippet from my index.js file: const express = require("express"); const path = require("path"); const http = require("http"); const socke ...

Align container to the very edge of the browser window

I am having an issue with a div that extends to the edge of the browser, and I want to add content that is aligned to the center of the page within a container. I tried using Bootstrap by creating a row with col-md-6 filled with a background color, but whe ...

My jQuery function only activates when I manually adjust my window size to mobile, but doesn't work when I refresh the

I'm currently working on a code snippet to dynamically add and remove classes on mobile devices. It seems to function properly when I manually resize the browser window to mimic a mobile screen, but upon refreshing the page, it stops working. $(docum ...

Solving the puzzle of complex polymorphic object model deserialization in Java Jackson: Facing the JsonMappingException error – Unexpected token (START_OBJECT) instead

I am working with a hierarchy of objects described as follows: A B extends A C extends B D extends B E extends C F extends A and contains a reference to A The annotation for class A is defined as: @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS,include=Jso ...

Verify if the value present in the array exists and is less than the succeeding value within the same array

In the task at hand, I am tasked with verifying whether a value entered into an input field exists in an array and is smaller than the subsequent value in the same array. The purpose of this check is to adjust the quantity of items in a shopping cart. Cor ...

Arranging a collection of tiny divs or images side by side

Is there a way to stack multiple divs next to each other in a responsive manner, without causing any skew? I want them to line up neatly and adjust properly on different screen sizes. Should I consider using images for this or is there another method to ac ...

Unable to show input in Javascript HTML

Every time I try to run this code on my webpage, the buttons do not seem to respond when clicked. I am aiming to have the user input for full name, date of birth, and gender displayed in the text box when the display button is clicked. When the next butt ...

Check each field in a loop and if validation for any field fails, then return false using jQuery

Having trouble resetting the flag variables when it comes to form validations. I seem to be missing something crucial. I'm dealing with a form that has multiple text fields. I want to validate each field on blur and prevent form submission if any val ...

Is there a way to customize the primary column in Material-table?

I am utilizing the Material-table and I am in need of rearranging the column index to be at the end of the table as the default position for this column (actions) is at the beginning. Here is the code snippet for the table: <MaterialTable title=" ...

Spinning a point around the center on a canvas

My friend and I are in the process of creating a game, and we've reached the point where we want to implement a radar system. While we have successfully designed a basic radar that displays everything in the correct positions, our next challenge is to ...

In what way does this operate in an asynchronously?

As a newcomer to node.js and asynchronous JS, I am grappling with interpreting it correctly. Specifically, I am trying to grasp the mechanics behind this snippet of code: var fs = require('fs') var filedir = process.argv[2] function doneReading ...

Utilize sinon with a vue action to create a mock class for unit testing puposes

I need to verify if my 'SIGN_IN' action is functioning correctly. This is how my action currently looks (not the most refined implementation but it's what I have at the moment): // actions.js import { SIGN_IN, SIGN_OUT, SET_AUTH_TOKEN, C ...

Utilize jQuery and $.get to send data to a php page

I am facing an issue with my datepicker form using jQuery UI. I need to select a date and then send that selected date to a PHP file. Here is the code I have attempted, but it seems to not work: $( "#datepicker" ).datepicker({ onSelect: function(dateT ...

Error: The module you are trying to import from the package is not found. Please check the package path and make sure that

I encountered an issue when trying to compile a code in Reactjs. As a beginner in Reactjs, I'm struggling with this. Module not found: Error: Package path ./cjs/react.development is not exported from package /Users/mansi/letsgrowmore/to-do-list/my-rea ...

Encountered a surprising error while writing JSX code in the IDE: "unexpected token <" - what could be

I seem to be encountering an error in my IDE where it's displaying 'Unexpected token <'. The React code snippet below is triggering this error message which states that adjacent JSX elements must be wrapped in an enclosing parent tag. How ...

The background image cover does not display properly on screens with taller resolutions

Hey there! I've been using the standard background cover CSS code for my project, which usually does the trick. Here's the code: -webkit-background-size: cover !important; -moz-background-size: cover !important; -o-background-size: cover !im ...

"Ajax has the ability to alter the vertical scroll position

I have a quick query for you. Can anyone assist me in understanding why the page scroll resets to the top after an Ajax request? I suspect it has something to do with JQuery. There isn't much information available online, so I'm reaching out for ...

Searching with Neo4j, Java and the Angular library for accurate

When my web application is opened, it displays a form where users can input a list of comma-separated usernames. The application then queries the neo4j database to fetch information for each username. I am interested in implementing autocomplete functiona ...