The function of slidetoggle is malfunctioning when clicked

I currently have two dynamic containers with one displaying grouped boxes, quantities, and totals, while the other shows detailed information.

Both container values are added dynamically at runtime. By default, the grouping container is displayed on the page.

The values are fetched using jQuery to showcase them in a "preview" table located outside the div containers.

I am attempting to utilize slidetoggle functionality to switch between the containers upon clicking a link, which works smoothly. However, I am encountering issues with the table as the values appear incorrect initially and upon subsequent clicks of the link.

For reference, you can view my current jQuery script here: [My jsfiddle][1]

Below is the test markup:

<div class="ToogleGroupContainer">
<div class="shipToSingleSection">
    <div class="shoppingCart_Category">
        <div class="shoppingCart_Box">Box#01</div>
        <div class="shoppingCart_qtyArea">
            <div class="shoppingCart_qtyLabel">QTY</div>
            <div class="shoppingCart_qty-select">5</div>
        </div>
        <div class="shoppingCart_Price_Each">Each 26.99</div>
        <div class="shoppingCart_SubTotal_Each">Price 134.95</div>                
    </div>
    <hr />
</div>
<div class="shipToMultipleSection" style="display:none;">
    <div class="shoppingCart_Category">
        <div class="shoppingCart_Box">Box#01</div>
        <div class="shoppingCart_qtyArea">
            <div class="shoppingCart_qtyLabel">QTY</div>
            <div class="shoppingCart_qty-select">1</div>
            <div class="shoppingCart_qty-select">1</div>
            <div class="shoppingCart_qty-select">1</div>
            <div class="shoppingCart_qty-select">1</div>
            <div class="shoppingCart_qty-select">1</div>
        </div>
        <div class="shoppingCart_Price_Each">Each 26.99</div>
        <div class="shoppingCart_SubTotal_Each">Price 26.99</div>

    </div>
    <hr />
</div>
</div>
<table class="tableOrderPreviewTotals" border=1>
<tr>
    <td class="yord_hdr">Item</td>
    <td class="yord_hdr_center">QTY</td>
    <td class="yord_hdr_center">Each</td>
    <td class="yord_hdr_center">Price</td>
</tr>
<tr>
    <td class="yord_line_item" stlye="display:none;">
        <div id="itemOrderPreview"></div></td>
    <td class="yord_line_qty" stlye="display:none;">
        <div id="qtyOrderPreview"></div></td>
    <td class="yord_line_right" stlye="display:none;">
        <div id="priceEachOrderPreview"></div>
    </td>
    <td class="yord_line_right" stlye="display:none;">
        <div id="subtotalEachOrderPreview"></div>
    </td>   
</tr>
</table>

<br/>
<a href="" class="shipToMultipleLink">Ship Order to Multiple Addresses</a>

It's apparent that there are inconsistencies in the data presentation. The goal is to show values accurately based on which container is active on the page.

The desired default display should be:

Item QTY Each Price

Box#1 5 26.99 134.95

Once the "Ship to multiple addresses" link is clicked (toggled), the table lines should adjust accordingly:

Item QTY Each Price

Box#1 1 26.99 26.99

Box#1 1 26.99 26.99

Box#1 1 26.99 26.99

Box#1 1 26.99 26.99

Box#1 1 26.99 26.99

Upon toggling back to the "Ship to single address" mode, the table line should revert to the default display:

Item QTY Each Price

Box#1 5 26.99 134.95

Any assistance provided will be highly appreciated!

Thank you for your help.

Answer №1

give it a shot

  $(document).ready(function () {
        $('.shipToMultipleLink').click(function () {
            $('.shipToMultipleSection, .shipToSingleSection').slideToggle('fast');
            return false;

        });

            $.each($('.shipToMultipleSection .shoppingCart_qty-select'), function () {
                $("#itemOrderPreview").append($(".shoppingCart_Box").html()).append("<br>")
                $("#qtyOrderPreview").append($(this).html()).append("<br>");

                var each = $('.shoppingCart_Price_Each').html().replace(/Each/,"")
                $("#priceEachOrderPreview").append(each).append("<br>");

                var price = +each * $(this).text()
                 $("#subtotalEachOrderPreview").append(price).append("<br>");
            });

      var h = '<tr class="singleLine" style="display:none;">'
      /* edited */
      h += '<td>' + $(".shoppingCart_Box").html() + '</td>'
      h += '<td>' + $(".shoppingCart_qty-select").html() + '</td>'
      h += '<td>' + $('.shoppingCart_Price_Each').html().replace(/Each/,"") + '</td>'
      h += '<td>' + $('.shoppingCart_SubTotal_Each').html().replace(/Price/,"") + '</td>' 
      h += '</tr>'
      $(".tableOrderPreviewTotals tr:last-child").after(h)

    });

jsfiddle: http://jsfiddle.net/alemarch/k3t0cbcc/4/ is this what you're looking for?

Answer №2

Wow, things are a bit messy here..

Let's start by troubleshooting your jQuery code:

 $('shipToMultipleLink').hide();
 $('shipToSingleLink').show();

This should be corrected to:

 $('.shipToMultipleLink').hide();
 $('.shipToSingleLink').show();

After fixing that, make sure to tidy up the rest of your code and provide a clearer explanation of your issue so we can assist you better.

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

What is the best method for assigning real-life units, such as centimeters or millimeters, to a dimension

Is there a way to set the height of an img in millimeters, regardless of the device being used? Currently, setting the CSS rule height: 10mm; actually translates to height: 37.8px, as explained in this link: http://css-tricks.com/the-lengths-of-css/ If p ...

The building of the module was unsuccessful due to a failure in the babel-loader located in the webpack module of Nuxt

While working on my Nuxt.js project, I encountered a warning error that I couldn't resolve even after searching for a solution. If anyone can assist me with this issue, it would be greatly appreciated. The error message is as follows: ERROR in ./.nu ...

Combining user input with React's useState function

I have a form with three Quantity inputs. (Although more can be added dynamically, let's keep it simple for now and stick to three.) | - | - | Quantity | |---|---|----------| | - | - | 3 | | - | - | 4 | | - | - | 5 | Now, I want ...

Running a child process within a React application

I'm currently in search of the best module to use for running a child process from within a React application. Here's what I need: I want a button that, when clicked, will execute "npm test" for my application and generate a report that can be r ...

Guide on storing a promise response in an external object in VUE

I am currently working on integrating the higchart network graph in vue. Everything seems to be functioning properly with static data. However, when I attempt to retrieve data using axios, it fails to work. export default { data() { return { ne ...

What is the best way to incorporate text below grid columns in html?

Can you show me how to insert text below a column using the grid system? Here is what I am trying to accomplish Here is the code I have created: .grid{ display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-auto-rows: 240px 240px; row-g ...

The information submitted through the form is not displaying on the console after being gathered using the post method in Express

When attempting to add products using a form on the admin page, the data (including an image file) is not being displayed in the console as expected. Instead, the following message appears: "{} POST /admin/add-product - - ms - -" Below is th ...

Activate a button by simulating a click event through a shortcut key with the help of a

I have incorporated the hotkeys plugin to enable shortcut functionality on my website. Currently, I am looking for a way to automatically trigger a button click when any shortcuts are pressed. hotkeys.add({ combo: 'alt+1', callback: function (da ...

Guide to: Implementing Radio Buttons to Adjust Image Opacity

How can I dynamically change the opacity of an image using JavaScript when a radio button is clicked? I attempted to achieve this using a forEach function, but the current code causes all images to instantly switch to opacity 1; Additionally, after refre ...

Is it possible to use JavaScript to save and preserve the current state of a webpage?

I'm looking for a way to retrieve the entire content of a webpage using JavaScript and then send it to a server script for saving. This action should be triggered after the user has made some changes to the page using AJAX and other JavaScript tools. ...

What could be causing the malfunction of DirectionalLight in three.js?

While working on a project in three.js to create a ring, I encountered an issue with lighting. The camera setup is fine, but I'm facing difficulties with lighting. Below is my code snippet: <script src="three.js"></script> <sc ...

The object NativeModules from the 'react-native' requirement is currently empty

At the top of one of the node_modules utilized in my project, there is a line that reads: let RNRandomBytes = require('react-native').NativeModules.RNRandomBytes However, I've noticed that require('react-native').NativeModules ...

Issue: Unable to utilize import statement outside a module as per the guidelines of the vue-test-utils official tutorial

I'm struggling to get Vue testing set up with vue-test-utils and jest. I followed the installation guide at https://vue-test-utils.vuejs.org/installation/#semantic-versioning, but can't seem to figure out what's wrong. Here's what I&apo ...

Filtering a list in AngularJS based on a property of an object with a filter applied on it

I am facing an issue with filtering a list of objects based on their properties using the filter filter. The problem arises when certain object properties have filters applied to them that alter their displayed format (such as the formatDate filter in the ...

How come characteristics of one particular div element are transferring to unrelated div elements?

Recently, I created a small practice website and encountered an issue that is quite frustrating. Here's the div structure I used: <div class="work-process"> <div class="container" align="center"> <div class="col- ...

The issue of drop shadows causing links to not work properly in Internet Explorer

I am currently working on a website design that features a fixed menu positioned behind the body. When the menu icon is clicked, some jQuery code shifts the body to the left. To create the effect of the fixed menu being positioned underneath, I have added ...

Issues with implementing the Skeleton feature in the Alert module

Having an issue with a Skeleton component not taking full width inside an Alert component. It seems to be occupying less space than expected. https://i.stack.imgur.com/cMLEy.png Here is the relevant code snippet: <Alert icon={<NotificationsIcon s ...

Creating a unique tooltip component for ag-grid with the use of Material tooltips

I am facing an issue with the current angular ag-grid tooltip example, as it is not functioning properly. https://stackblitz.com/edit/angular-ag-grid-tooltip-example-fb7nko Utilizing Javascript/typescript in Angular 8 with the latest ag-grid release. I h ...

Discover the inner workings of the code below: set a variable called "start" to the current time using the new Date().getTime() method. Create a loop that continuously checks if

I'm having trouble understanding how this code snippet works. Can someone please provide a demonstration? Thanks in advance.... It seems that the code is subtracting 'start' from the current date with new Date, resulting in 0 which is less t ...

Stopping React component click event from bubbling up to document

How do I prevent a click event in a React component from bubbling up to the document? There seems to be an issue that needs fixing! Let's see if we can resolve it on our own. I have a box component with a click event listener, and some link compone ...