preventing a button from triggering the same event repeatedly

Currently, I am using a script that adds a new row after the tr where my button is located. Unfortunately, I do not want the same button to generate more than one tr. Here is the script in question:

<script>
$(function() {
    var done;
    $('table.listtable td input.selectseat').click(function() {

        if( done ) return;
        done = true;
       $(this).parent().parent().after('<tr class="seating"><td colspan="6"></td></tr>');
    });
});
</script>

The issue I am facing is with multiple buttons in sequence. The current script prevents me from adding another row from a different button after already producing a row with a previous button click. I have a series of rows each featuring a "select seats" button. My goal is for each individual button press to only create one row, but currently, all buttons are restricted from generating additional rows.

Specifically, this section of code:

if( done ) return;
done = true;

was included to stop a button clicked once from creating any more rows, yet it seems to prevent any buttons from adding a new row after just one click. Any assistance on resolving this issue would be greatly appreciated.

Answer №1

This is the perfect use case for the one method.

When you need a particular function to run only once upon a click event on an input element within a table, you can use the following code snippet:
$('table.listtable td input.selectseat').one('click', 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

The issue of jQuery's .last() function triggering multiple times with just a single click on the last

I currently have a set of tabs containing radio buttons and I need to trigger an event when the last option is selected (meaning any radio button within the final tab, not just the last radio button). Below is the HTML code: <div id="smartwizard" clas ...

Utilizing jQuery for seamless and ongoing animation effects

I have encountered many questions regarding jquery animation, but so far I haven't come across a solution that works for my specific case. While I have seen numerous examples of using the .animate function with a "complete" parameter callback, none o ...

Scrolling automatically to a DIV is possible with jQuery, however, this feature is functional only on the initial

I'm currently working on a Quiz site and I've encountered a puzzling issue. Since the explanation only appears after an answer is selected - essentially a "hidden" element, I decided to wrap that explanation into a visible DIV. The code I'v ...

Conceal a div and label after a delay of 5 seconds with a JavaScript/jQuery function in C#

Here is a sample div: <div class="alert alert-success alert-dismissable" runat="server" visible="false" id="lblmsgid"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> ...

Ways to extract data from form inputs with the help of jQuery

My HTML form allows users to enter data and upon submission, I need to use jQuery to capture the entered values. <form class="my-form needs-validation" method="POST"> <input type="text" id="firstName" name="First Name"> <input type="tex ...

Is it possible to use jQuery to dynamically set the line-height of nested objects in CSS?

I am in the process of creating a horizontal family tree. Where I'm At - I am currently working on setting the CSS Line-Height to vertically center each item based on nested content. The Issue - The Line-Height value for nested items keeps growing e ...

Adding options to a dropdown menu dynamically while editing a form with the help of javascript

I have a dropdown like in below:- <form name="depositForm" action="<?php echo site_url('ajax_funds/deposit_funds'); ?>" id="depositForm" class="page-form-main form-horizontal " autocomplete="off" method="post"> <div class="form- ...

Error in CSS Header - Position Shifts when Hovered Over

UPDATED HEADER HTML/CSS I am currently working on the header section of my website at . I encountered a bug where, upon hovering over the links in the header from right to left, the positions of the links become disorganized. I am unsure of what might be ...

Adding and removing classes in JQuery based on a specific div ID

Is there a way to use addClass() and removeClass() on divs with different class names, identified by their unique ids? Any suggestions on how to make this functionality work? Here is the JavaScript code snippet: $(document).ready(function() { $("#bu ...

Numerous column pictures that occupy the entire browser screen

I am looking to create a grid of clickable images that expand to cover the width of the browser window without any space on either side. Ideally, I would like each image to be 180x180px in size, but if it's easier they can resize based on the browser ...

Using JQuery to dynamically add items to an ASP.NET Repeater on the client-side

Here is the repeater code that I am working with: <asp:Repeater ID="_List" runat="server" DataSource="<%# GetList() %>"> <HeaderTemplate> <table class="dataList"> <tr> <th>Name</th> </tr> ...

Jquery code not responding to ajax callback

i have the following two functions defined: function populateTableData(){ jQuery('select[name="drg-select"]').change(function() { drgValue=jQuery(this).val(); url="http://localhost/ur ...

How to include a Partial View or Child Page into a Master Layout using jQuery/Ajax in ASP.NET MVC

Currently, I am in the process of working on a CRM Admin panel that utilizes a left-hand side panel for navigation to various pages. Within this panel, there are buttons such as Home, About, and Contact. To streamline the layout, I have implemented a mast ...

Form submission is not possible while the login form is active

I'm encountering an issue where I am unable to trigger - ng-submit or ng-click on this code except for the local onclick function on the login button. If you have any insights or solutions, please share them: <form ng-submit = "login.submitLogin ...

jQuery Toggle and Change Image Src Attribute Issue

After researching and modifying a show/hide jQuery code I discovered, everything is functioning correctly except for the HTML img attribute not being replaced when clicked on. The jQuery code I am using: <script> $(document).ready(function() { ...

Using jQuery for dynamically updating a dropdown menu

There's a form where users can submit information about systems that need updates. Users can add systems dynamically to the form, making unique IDs unavailable. They have to choose a platform (unix, hp, wintel, etc) and then select the corresponding m ...

Assign a value to a locally scoped variable within an iteration in Angular 2

Within my Angular code, I have the following HTML snippet: <span *ngIf="ControllerType?.AttributeID =='Controller Type'"> <select multiple name="ControllerType.Default" [(ngModel)]="Contro ...

Storing jQuery output in a global variable can be achieved by assigning the result

Take a look at the Code snippet below - $(function() { $.fn.getPosition = function() { var results = $(this).position(); results.right = results.left + $(this).width(); results.bottom = results.top + $(this).height(); return results; } ...

Transforming jQuery Object into a String after making an AJAX request

If I were to submit a form with some text in the value of user_input, let's say "I am free," through AJAX, and it comes back to me as a string. Once it becomes an Object, how could I convert it back into a string format? Thanks, <!DOCTYPE HTML> ...

What are the reasons for the dynamic exclusion of an element in Angular?

Can someone help me figure out why my data is not being added dynamically using ng-repeat? I have entered the "name" which should be added to the data, but it is not displaying in the UI. You can see the issue in this demo app.controller("studentcntr", ...