Tips for concealing the final click (add) tab after it has been clicked and revealing the hidden (add) button when the user clicks on the remove button

I have created a form where users can add tests. Everything is working smoothly but I want the previous "Add Another Test" field to be removed when the user clicks on it and a new field to be shown.

Everything is running well, but the issue is that when the user clicks on "Add Another Field," the previous field is not hidden. Also, when the user clicks on "Remove," the previous field should hide automatically.

Could you please review my code? Thank you.

   ! function(a) {
   "use strict";
    a(function() {
    var b = a(".wpcf7-field-groups");
    b.length && (b.each(function() {
        a(this).data("group-model", a(this).find(".wpcf7-field-group").eq(0).clone())
    }), a("body").on("wpcf7-field-groups/change", ".wpcf7-field-groups", function() {
        var b = a(this).find(".wpcf7-field-group");
        b.each(function(b) {
            a(this).find(".wpcf7-field-group-remove").toggle(b > 0);

            var c = b + 1;
            a(this).find("[name]").each(function() {
                var b = a(this),
                    d = b.closest(".wpcf7-form-control-wrap"),
                    e = b.attr("name"),
                    f = e.indexOf("[]") > -1,
                    g = e.replace("[]", ""),
                    h = g.replace(/__[0-9]*/, "") + "__" + c;
                d.length && !d.hasClass(h) && d.removeClass(g).addClass(h), h += f ? "[]" : "", b.attr("name", h)
            })
        }), a(this).find(".wpcf7-field-group-count").val(b.length)
    }), b.trigger("wpcf7-field-groups/change"), a("body").on("click", ".wpcf7-field-group-add, .wpcf7-field-group-remove", function() {
        var b = a(this),
            c = b.closest(".wpcf7-field-groups");
        if (b.hasClass("wpcf7-field-group-add")) {

            var d = c.data("group-model").clone();
            c.append(d), b.trigger("wpcf7-field-groups/added");
        } else b.trigger("wpcf7-field-groups/removed"), b.closest(".wpcf7-field-group").remove();





        return c.trigger("wpcf7-field-groups/change"), !1
    }))
})
 }(jQuery);

This is my full working CodePen code. Please check and let me know how to fix this :)

CodePen Link

Thank you in advance

Answer №1

To enhance your function where you attach the click event to your element, simply incorporate the following line of code:

 e.currentTarget.style.display = "none";

Make sure to pass e as an argument when calling the function.

Here is an example implementation:

if (b.hasClass("wpcf7-field-group-add")) {
        e.currentTarget.style.display = "none"; //INSERT THIS LINE
        var d = c.data("group-model").clone();
        c.append(d), b.trigger("wpcf7-field-groups/added");
}

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

A guide to saving an ArrayBuffer as a file using JavaScript

I am currently developing a file uploader for the Meteor framework. The approach involves breaking down the file on the client side from an ArrayBuffer into small packets of 4096 bits, which are then sent to the server through a Meteor.method. The abridge ...

Issue encountered with websocket connection while attempting to include dependencies

My current project involves integrating charts for the graphical component using React within an Electron software. I've added interaction with buttons (sections) to insert different data into the graphs based on user clicks on one of the sections. Th ...

Modifying data on the fly for a Highcharts series

My chart is currently functioning well with data in the options. However, when I leave the data empty for a series and attempt the following code (in order to change the data based on a click event), it fails to work. Any suggestions on how I can rectify ...

Create a Discord.js bot that automatically deletes any URLs that are posted in the server

Seeking advice on how to have my bot delete any URLs posted by members. I am unsure of how to accurately detect when a URL has been shared, especially since they can begin with https, www, or some other format entirely. Any insights would be greatly apprec ...

Ways to choose a designated element without relying on ids or classes

I am looking to create an on-click function for each button, so I require a method to select each one individually without relying on IDs <div class="col"> <div> <b>Name:</b> <span ...

Methods for presenting text from an object array using Angular

I'm running into a bit of trouble with getting my text to show up properly in my code. In the HTML, I have <td>{{cabinetDetails.cabinetType}}</td> and my data source is set as $scope.cabinetDetails = [{cabinetType: 'panel'}]; De ...

Tips for addressing lag problems in my Three.js game as time progresses

My game in Three.js doesn't start off with any lag, but after a few minutes, the performance begins to slow down on computers running it. I've tried reviewing my code and checking my arrays, adjusting values to troubleshoot, but so far, nothing s ...

Tips on creating adaptable images for mobile viewing

My coding conundrum involves the use of two columns - one for an image and the other for a description of that image. However, when viewing my site on mobile devices, the image is cut off at only half its height. Adjusting both columns to col-sm-6 results ...

Utilizing Jquery Datatables for PHP-based server-side processing with MSSQL integration

I found a helpful guide on Coder Example to implement server-side processing with datatables in PHP, MSSQL, and AJAX. After customizing the code to fit my needs, here is how my index page looks like: <!DOCTYPE html> <html> <title>Da ...

Mastering MochaUI: A Comprehensive Guide

Can anyone direct me to a comprehensive tutorial for MoachaUI? I have a strong understanding of CSS, basic knowledge of JavaScript, and experience with PHP. The MoachaUI user interface has caught my interest, but I'm struggling to find a suitable lear ...

Display issue with grid-template-column not appearing

My grid column isn't working in my Christmas matching game project. I'm trying to create additional card columns using grid template columns, but the other three columns are not displaying as intended. They should be positioned right next to "Tim ...

arrow function implemented in a React hook for handling onClick event

From my understanding, placing an arrow function in the JSX creates a new reference of a new function each time it is triggered. For example: <p onClick={() => handleClick() /> In older versions of React with classes, we could do this: <p onCl ...

Ag-Grid: A Guide to Storing and Restoring Columns with Filter Settings

Ag-Grid allows us to easily filter columns. One requirement I have is to save the column filter order in a SQL database backend. I have implemented a method for saving the column order, but it doesn't seem to work properly. onGridReady: function(par ...

The Node module's package.json does not have a main "exports" defined

After recently adding the zx NPM package to my project, I encountered a puzzling issue. The installation went smoothly, and I proceeded to import it into my code: import { $ } from 'zx' (async () => { await $`mkdir test` })() However, u ...

Warning: Fastclick alerting about an ignored touchstart cancellation

Having an issue with a double popup situation where the second popup contains selectable fields. Below is the code snippet I am using to display the second popup: $("#select1").click(function(e) { e.stopPropagation(); var tmplData = { string:[& ...

Using PHP to upload images through AJAX increases efficiency

Worked tirelessly on this script all night still unable to fix the bug. The issue is that when I select an image and click upload, it uploads the current image file. Then, if I select another image and click upload, it uploads two new files along with the ...

Encountering difficulties triggering the click event in a JavaScript file

Here is the example of HTML code: <input type="button" id="abc" name="TechSupport_PartsOrder" value="Open Editor" /> This is the jQuery Code: $('#abc').click(function () { alert('x'); }); But when I move this jQuery code to a ...

What is the process for browserifying the net.Socket module in Node.js?

I'm exploring ways to connect and query my MS SQL database from JavaScript in a web browser (specifically Chrome, not IE as I don't want to use ActiveX controls). I came across this Node library called Tedious and Browserify to help with this tas ...

What is the best way to extract plain text with JQuery?

I found this Html code snippet: <div class="form-group row"> <label id="lb_size" class="col-lg-3 col-form-label"> <span class="must-have">****</span> Size </label> </div> My goal is to ext ...

Utilizing ng-options within an ng-repeat to filter out previously chosen options

Facing a simple problem and seeking a solution. I am using ng-repeat to dynamically create select boxes with ng-options displaying the same values. The ng-repeat is used to iterate through model options. <div data-ng-repeat="condition in model.condit ...