What is the best way to loop through ul elements inside a div and hide each one separately upon submission?

I have a question regarding my node application that uses express. I have a view function that generates a list of inactive companies, with each company having two submit input types: "Active" and "Delete". My goal is to be able to click on the submit button and hide only that particular ul element. Unfortunately, I've been struggling to iterate over each element individually without hiding all of them at once. Here's the code snippet for my view function:

function generateInactiveCompany(companyObject) {
    return `
        <ul class="companyinfo">
            <li class="list-info">${companyObject.company_type}</li>
            <li class="list-info">${companyObject.company_name}</li>
            <li class="list-info">${companyObject.company_location}</li>
            <li class="list-info">${companyObject.company_phone}</li>
            <br>
            <li class="list-buttons">
                <form action="/activeList" method="POST" class="myform">
                    <input type="hidden" name="companyId" value="${companyObject.id}">
                    <input type="submit" value="Active">
                </form>
                <form action="/deletecompany" method="POST">
                    <input type="hidden" name="companyId" value="${companyObject.id}">
                    <input type="submit" value="Delete">
                </form>
            </li>
            <br>
        </ul>
    `
}

function generateInactiveCompanyList(arrayOfCompanies){
    const companyItems = arrayOfCompanies.map(generateInactiveCompany).join('');

    return `
        <div class="list inactive-list">
            ${companyItems}
        </div>
    `
}

module.exports = generateInactiveCompanyList;

I have tried using jQuery to target and hide individual ul elements upon form submission, but it currently hides all of them simultaneously:

$(document.body).submit(function() {
    $('.companyinfo').each(function(i) {
        $(this).hide();
    })
})

I have been stuck on this issue for quite some time and would highly appreciate any assistance. Thank you!

Answer №1

By using the selector .companyinfo, you are hiding all elements at once because it targets every element with the class companyinfo, essentially hiding all companies simultaneously.

To address each company individually, consider adding unique ids to the ul elements like this:

<ul id="companyinfo_${companyObject.company_name}" class="companyinfo">
.

Next, create a new function called hideCompany() to replace the existing

$(document.body).submit(function()
part:

function hideCompany(companyname) {
    $('#companyinfo_' + companyname).hide();
}

Finally, update the

<input type="submit" value="Delete">
line to include an onclick event that calls the
hideCompany('${companyObject.company_name}')
function.

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

Reset the form value in React using Material UI components

Currently, I am working with React Material and hooks, attempting to reset a material form. However, despite my efforts to adjust the state, I am not seeing any changes reflected. <form className="create-account-form" autoComplete="off" onSubmit={onSub ...

Are the intervals constantly shifting?

I have encountered an issue in my code where if an element is not currently doing something (specifically, "playing"), then I initiate the playing sequence. However, if it is already playing, I stop the action. While I have successfully implemented the sta ...

Using Laravel to dynamically load a post with a specific custom identifier through an asynchronous HTTP request

When I click on a post link, I want to load my post via AJAX. How can I send the post ID through AJAX? In my Blade file, there is a link: {{ link_to_route($post->type, $post->comments->count() . ' ' . t('comments'), arra ...

The issue of button onclick textfield validation malfunctioning

Within this container, there is a text field identified as 'codeCityId' along with a button that triggers an onclick method. When the button is clicked, the function will verify the input. function click()={ var valid = $('#codeCityId' ...

Issues with jQuery causing responsive CSS triangles to fail implementations

Recently, I stumbled upon the interesting concept of CSS triangles created using element borders. I decided to incorporate this design into one of my custom Wordpress themes, but I encountered a problem. I wanted to add a triangle at the bottom of a div, l ...

Potential Javascript timing problem encountered during webpage initialization – involving the implementation of a dynamic bootstrap progress

I have limited knowledge of javascript, but I stumbled upon this amazing fiddle that I would like to incorporate into my project: http://jsfiddle.net/5w5ku/1/ The issue I am facing is that I want it to persist for a duration of ten minutes. Despite atte ...

Creating a clickable link using a for loop to set the destination URL

I'm currently working on a project where I want to dynamically populate a set of buttons with the correct hrefs. However, the setLink() function is not returning the expected value (which should be something like localhost:44369/Timeline?d=2020/7/3). ...

What is the best way to display MySQL data in a jQuery dialog box?

I have a question regarding implementing dynamic content from a database into a jQuery UI Dialog box. Within my table, I am generating blog posts using PHP and MySQL. Each post has a link that allows users to view the content associated with it. The link ...

Navigating through parent modules while implementing layered routing within mongoDB

Currently, I'm in the process of developing a social networking platform specifically for enthusiasts who collect teddy bears. The site will enable users to create a personalized "collection" page where they can showcase individual profile pages for e ...

How do I overwrite this calculation using JQuery?

I have a website: link There are two pages on my site that have the same div elements... I want one page to perform a certain calculation on the divs, and another page to perform a different calculation... New JavaScript Code: jQuery(document).ready(fu ...

Rotating 3D cube with CSS, adjusting height during transition

I'm attempting to create a basic 2-sided cube rotation. Following the rotation, my goal is to click on one of the sides and have its height increase. However, I've run into an issue where the transition occurs from the middle instead of from the ...

Encasing common functions within (function($){ }(jQuery) can help to modularize and prevent

As I was creating a global JavaScript function and made some errors along the way, I finally got it to work after doing some research. However, while searching, I came across an example using (function($){ code here }(jQuery); My question is, what exact ...

Node.js server experiencing delays due to V8 processing constraints

REVISED I am currently running a nodeJS http server designed to handle uploads from multiple clients and process them separately. However, I have encountered an issue where the first request seems to block any subsequent requests until the first one is co ...

Initial request fails to retrieve cookie generated by Javascript

I created a shopping cart that generates a cart, adds items, and stores it in a cookie named 'cart'. However, when I click on a hyperlink that leads to the checkout page, my ASP.NET application is unable to access the cookie. The cookie only get ...

Unable to see Bootstrap 5.2 Modals in action with documentation demo

Here is an example that I copied and pasted from the documentation on https://getbootstrap.com/docs/5.2/components/modal/: <!-- Button trigger modal --> <button type="button" class="btn btn-primary" data-bs-toggle="modal&q ...

Tips for dividing Postgres information into JSON or object format for seamless integration with Express

I'm currently utilizing pg-promise for managing requests between Express and a Postgres database. Implementing pg-promise's any: db.any('select * from blog') .then(function (data) { console.log(data); }) .catch(function (err ...

jQuery registers the enter event, but does not proceed to trigger the enter action

Hey there, I've been experimenting with jQuery to capture the enter event. Something peculiar is happening though - after pressing enter in the text area, an alert pops up but the text gets entered only after that. Despite trying various solutions, I ...

what is the best way to create a wishlist feature using React.js

I'm working on creating a wishlist feature using React.js. My goal is to add an item to the wishlist when the user clicks the wish button, and mark it as liked by setting the boolean data "liked: true". Additionally, I want the item to be removed fr ...

Encountering an issue of Error 400 (Bad Request) while attempting to establish a connection between a socket in

I encountered an issue while attempting to integrate Electron and Python using web sockets (mainly as a learning exercise): Create a desktop app with Electron Accomplished Develop a Python program for monitoring certain things Accomplished Establis ...

Guide on displaying JSON data from a server on a webpage using Google Charts in real-time

Although I am not a JavaScript developer or an expert in AJAX, I consider myself a novice desktop developer. I would greatly appreciate it if you could guide me on how to connect an MCU that returns JSON data to a web page using Google gauge, running on th ...