Click the "Add to Cart" button to make a purchase

Recently, I've been working on modeling an add to cart feature using jQuery. However, I have encountered a small issue that I'm trying to troubleshoot. When I click the mybtn button, the model displays and functions correctly, but there seems to be a problem with the quantity not updating properly. Can anyone offer suggestions on how to tackle this? Thanks in advance.

<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.2/jquery.modal.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
<body>
  <div class="shoe single-item hvr-outline-out">
    <form method="post" action="#" class="cart">
      <input type="hidden" name="id" id="id" id="<%= doc._id%>">
      <input type="hidden" name="cmd" value="_cart">
      <input type="hidden" name="add" value="1">
      <input type="hidden" id="pname_<%= doc._id%>" name="shoe_item" value="<%=doc.product_name%>">
      <input type="hidden" id="amount_<%= doc._id%>" name="amount" value="<%=doc.price%>">
    </form>
    <button type="submit" id="myBtn1_<%= doc._id%>" data-id="<%= doc._id%>" value="<%= doc._id%>" class="shoe-cart pshoe-cart myBtn"><i class="fa fa-cart-plus" aria-hidden="true"></i></button>
  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <br/>
        <center>
          <table class="row"> </table>
        </center>
      </div>
    </div>
  </div>
</body>

<script type="text/javascript">
  $(document).ready(function() {
    $(".myBtn").click(function() {
      $('#myModal').modal('show');
    });
  });

  var count = 0;
  
  $(".myBtn").click(function() {
    var id = $(this).data('id');
    var p_name = $('.cart').find('#pname_' + id).val();
    var count1 = count++;
    var amount = $('.cart').find('#amount_' + id).val();
    var html = "<tr id='tr_'" + id + "><td>" + p_name + "</td><td>" + amount + "</td><td><input type='text' name='count' id='count_" + id + "' size='3' value='" + count + "'></td>/tr>";
    var quan = $('.row').find('#count_' + id).val();
    if (!quan) {
      $('.row').append(html);
    } else {
      $('#count_' + id).append(count);
    }
  });
</script>

Answer №1

Implement a click event once the modal is displayed

$('#myModal').on('shown.bs.modal', function () {
  $(".myBtn").click(function() {
     //place your custom code here
  });
})

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

Delete event once it has been directly added to fullCalendar

After adding an event directly to full calendar, I want to be able to remove the event when it is dropped. I add the id to the event object and when clicking on the event, my goal is to delete it. I have added an alert for the id, which displays correctly, ...

Choose every fourth row in the table

Is there a way to alternate the background colors of selected groups of 4 rows in a table? I want to make one group red and the next group blue. Any suggestions or ideas on how to achieve this? <table> <tr style="background-color: red;"> ...

Tips on utilizing React and Node to upload text as a file to Google Drive

Are you wondering how to incorporate React.js as the frontend and Node.js as the backend for uploading/reading files from Google Drive? In my attempts, I've tried writing a string as a text file and then uploading it to Google Drive by following this ...

Tips for displaying axios status on a designated button using a spinner

Utilizing a v-for loop to showcase a list of products retrieved from an API request. Within the product card, there are three buttons, one for adding items to the cart with a shopping-cart icon. I aim for the shopping-cart icon to transform into a spinner ...

What causes child margins to extend beyond the boundaries of div elements?

Can anyone shed some light on the advantages of CSS behavior that allows a child element's top and bottom margins to extend beyond the boundaries of its block-parent? Check out this fiddle for a straightforward example. The pink div is influenced by ...

Adding a div to a different webpage using jQuery

I am currently working on a small admin panel and I was curious if it is feasible to use jQuery to add a div box inside the index.php file after clicking a button in the panel? If this is possible, an example demonstrating how to do so would be greatly ap ...

AngularJS initiates an XMLHttpRequest (XHR) request before each routeChange, without being dependent on the controller being used

I'm currently embarking on a new project, and for the initial phase, I want to verify if the user has an active session with the server by sending an XHR HEAD request to /api/me. My objective is to implement the following syntax $rootScope.$on("$rou ...

Determine the specific element that triggered an event from a group of elements using Hammer

I am facing an issue with two HTML divs as shown below: <div class="div-class" data-val="1">Div 1</div> <div class="div-class" data-val="2">Div 2</div> <div class="div-class" data-val="2">Div 2</div> When using jQuery ...

Adding color to the header's top section and leaving the navigation bar untouched

A few days ago, I posted a question on this platform regarding customizing CSS related to search forms within Bootstrap. The response I received from Charlie was incredibly helpful in refining the code for my header and navigation elements. You can view hi ...

When utilizing custom modules in Node.js, is it more effective to require the module from within a function or at the top of the script?

I have developed a custom script that includes all of my common settings and functions. One specific function within this script takes a timestamp as input and outputs a human-readable date with a customized format using Moment.js. The custom script is st ...

Adjust the size of the font in an image created from text using a dynamic function

Creating stencil art is a fascinating process. I have successfully converted text to images, with each text box generating a separate line of the image on the browser. However, I am facing a challenge in implementing three different font sizes for each con ...

Unlock the potential of accessing data from the parent controller within a child controller in AngularJS

Is it possible to retrieve model data from the parent controller within a child controller? <div ng-controller="abc"> <div ng-controller="def"> <span> {{name}}</span> </div> </div> Can we access the val ...

Need to return false even though Jquery form check currently returns true

Upon form submission, the following check is executed: if((formData[4].value == 1 || formData[4].value == 2) && !formData[2].value) { alert('Please fill out the key field'); return false; } else { $.ajax({ url: "/aja ...

A clever JavaScript function generator encapsulated within an instant function nested within a jQuery ready statement

This code snippet features an immediate function enclosed within a jQuery ready function. $((function(_this) { return function() { document.write('called!'); }; })(this)); I am puzzled by where the resultant function ...

Display and conceal elements based on their data attributes

Struggling with toggling the visibility of elements within a div based on select value changes. I tried using data- attributes to target the elements for manipulation, but for some reason, my code isn't working as expected. Here's a simplified ...

JavaScript code encounters a math calculator script error

Am I overlooking something in my script? Everything seems to work fine when I click the minus (-) button, but when I press the plus (+) button, the calculator malfunctions. It displays 1, 11, 21, and so on, all ending with 1... Here is my code: functi ...

"Error: The chai testing method appears to be improperly defined and is not

I am trying to set up a Mocha and Chai test. Here is my class, myClass.js: export default class Myclass { constructor() {} sayhello() { return 'hello'; }; } Along with a test file, test.myclass.js: I am attempting to a ...

Arranging blocks within blocks? Placing additional text underneath a block

Looking to append some text at the end of a block called .label. Inside this block, there is a sub-element called .label_title, and below that is a picture/link enclosed in another block. Under the picture/link, I wish to include a short description while ...

Utilizing an Ajax request for a polling system

I am in need of adding a polling mechanism to call a web service from my webpage. To achieve this, I am attempting to utilize an ajax call within a javascript page. However, I am fairly new to both ajax and javascript. Below is the code snippet that I have ...

What is the best way to determine if a PHP string has identical consecutive letters?

Can you help me figure out why my code isn't working? $pattern_c_sap='/\.\-/'; $local='.................'; $local_array = explode( '', $local ); for($i=0; $i<=$local_length; $i++){ if(preg_match($pattern_c_ ...