Executing various onclick functions - Text Display

I've developed a code that includes onclick events to change the color of buttons and prevent them from being clicked twice. Additionally, I am looking to have data added to a table column/row when a button is clicked. For example, clicking button 3 should output its id data to a column/row. Subsequently, clicking button 1 would add its id data to the same row/column, effectively adding it to the top of the list.

<!DOCTYPE html>
<html>

<head>
  <script src="Https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    $(document).ready(function () {
      $(".button").click(function () {
        $(this).css('background', 'yellow');
      });
    });
  </script>
  <style>
    .button {
      background-color: #FFFFFF;
      /* White */
      border: 2px solid #f44336;
      color: black;
      padding: 10px 10px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 50px;
      border-radius: 50%;
      width: 80px;
      text-align: center;
      cursor: pointer
    }

    .disabled {
      background-color: #0000ff;
      /* Blue */
      color: white;
      cursor: not-allowed;
    }

    .button-clicked {
      background-color: #FFFF00;
      /* Yellow */
    }

    .buttonreset {
      background-color: #FF0000;
      /* Red */
      border: 4px solid #f44336;
      color: black;
      padding: 10px 10px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 20px;
      border-radius: 20%;
      width: 180px;
      text-align: center;
      cursor: pointer
    }

    td:not(:first-child) {
      padding-top: 5px;
      padding-bottom: 5px;
      padding-right: 5px;
    }
  </style>
</head>

<body bgcolor="black" text="white">
  <table width="100%">
    <tr>
      <td align="center"><button class="button" id="B1" onclick="myFunction(); 
this.disabled = true">1</button></td>
      <td align="center"><button class="button" id="B2" onclick="myFunction(); this.disabled = true">2</button></td>
      <td align="center"><button class="button" id="B3" onclick="myFunction(); this.disabled = true">3</button></td>
      <td>PUT ID DATA HERE IN ORDER OF BUTTON PRESSED
      </td>
    </tr>
  </table><br><br><br><br>
</body>

</html>

Answer №1

Do you desire this? You already possess a click function, all that is required is to .append() a new row to the table with the designated id. For instance, something along these lines:

$("tr").append("<td>" + $(this).attr('id') + "</td>");

Furthermore, kindly eliminate the

onclick="myFunction(); this.disabled = true"
from your HTML as it leads to an error.

Take a look at the snippet below for a working solution:

<!DOCTYPE html>
<html>

<head>
  <script src="Https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    $(document).ready(function () {
      $(".button").click(function () {
        $(this).css('background', 'yellow');
        $("tr").append("<td>" + $(this).attr('id') + "</td>");
      });
    });
  </script>
  <style>
    .button {
      background-color: #FFFFFF;
      /* White */
      border: 2px solid #f44336;
      color: black;
      padding: 10px 10px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 50px;
      border-radius: 50%;
      width: 80px;
      text-align: center;
      cursor: pointer
    }

    .disabled {
      background-color: #0000ff;
      /* Blue */
      color: white;
      cursor: not-allowed;
    }

    .button-clicked {
      background-color: #FFFF00;
      /* Yellow */
    }

    .buttonreset {
      background-color: #FF0000;
      /* Red */
      border: 4px solid #f44336;
      color: black;
      padding: 10px 10px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 20px;
      border-radius: 20%;
      width: 180px;
      text-align: center;
      cursor: pointer
    }

    td:not(:first-child) {
      padding-top: 5px;
      padding-bottom: 5px;
      padding-right: 5px;
    }
  </style>
</head>

<body bgcolor="black" text="white">
  <table width="100%">
    <tr>
      <td align="center"><button class="button" id="B1">1</button></td>
      <td align="center"><button class="button" id="B2">2</button></td>
      <td align="center"><button class="button" id="B3">3</button></td>
      </td>
    </tr>
  </table><br><br><br><br>
</body>

</html>

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

Switch the jQuery overlay image upon clicking a button

Is there a way to dynamically change the overlay image of a jQuery-ui dialog using a button click from inside the dialog? I attempted to do so in the code snippet below, but unfortunately, the overlay image does not update. It seems that I need to manipu ...

Retrieve the nearest identifier text from the tables

I have a table on my webpage with some data: <tbody id="carga"> <tr> <td>1</td> <td id="nombre">esteban</td> <td id="apellido">aguirre</td> <td>N/A</td> <td>N/A</td ...

Animating the left and right positioning of a single element using CSS transitions

I am currently working with the following CSS code: #masthead { transition: left linear 0.25s; -moz-transition: left linear 0.25s; -o-transition: left linear 0.25s; -webkit-transition: left linear 0.25s; -ms-transition: left linear 0.2 ...

Optimizing Angular for requireJS deletion

We recently developed an Angular directive that utilizes blueimp-fileupload. Everything seems to be working fine until we decided to optimize our code using requireJs. After running the optimizer, we encountered the following error: Error: cannot call m ...

Encountering a CORS issue specifically on the client side of a Next.js application when interacting with an API gateway

I've been struggling with this issue for a week now and can't seem to fully describe it. I have a FastAPI server running as a Lambda connected to API Gateway. https://i.stack.imgur.com/S5Zx9.png Both FastAPI and API Gateway have CORS enabled, b ...

Tips for troubleshooting a node module that is part of a build process

When working on my applications, I often rely on the NPM package.json to handle my build tools. However, I've come across a module that seems to have a bug. I'm eager to debug it, but I'm unsure how to do so within the context of the build t ...

Tips for disabling automatic scrolling when a browser is reloaded using JavaScript

In today's world, most browsers automatically adjust the window scroll position upon page refresh. While this is generally a helpful feature, I find myself in a situation where I need to reload a page and direct focus to a different part of the page t ...

Is it possible to implement smooth scrolling in HTML without using anchor animation?

Is it feasible to implement a more seamless scrolling experience for a website? I'm referring to the smooth scrolling effect seen in MS Word 2013, but I haven't come across any other instances of this. I've heard that AJAX can make such th ...

Ways to create a unique animation for reducing the width of a div in a specific direction?

Currently, I have managed to animate the decrease in width of a div element. However, it is currently decreasing from right to left, and I would like it to decrease from left to right instead. Can someone please provide guidance on how to achieve this? ...

Can different versions of Node be used simultaneously for various Node scripts?

Currently, I am utilizing nvm. Can a specific node version be used for a particular script? For instance... Using node 6 forever start -a -l $MYPATH/forever.log -e $MYPATH/err.log -c "node --max_old_space_size=20" $MYPATH/script_with_node_version_6.js U ...

Display the variance in values present in an array using Node.js

I am facing a challenge and need help with printing arrays that contain both same and different values. I want to compare two arrays and print the values that are present in both arrays in one array, while also creating another array for the differing val ...

Executing a single query in PHP using multiple WHILE loops

Recently, I've been working on a PHP page that interacts with an Oracle Database through SQL queries. SELECT * FROM TABLE1; My challenge now is to run five WHILE statements based on the value of the recordset. Ideally, it would look something like t ...

Lock the first row and a few lines in place for an HTML table

I'm facing a layout challenge on my webpage. I have a header line followed by a table, and I want both the header line and the top row of the table to be frozen while allowing the rest of the table to scroll. Despite trying to use 'position:fixed ...

Is it possible to use a link's URL as the action for a modal form submission?

I am writing a Rails application that requires users to store credential information in the database... ...

Utilizing Vue.js to add functionality for navigation buttons allowing users to move between survey questions

In my Vue.js component, I've written code to show survey questions in a mobile app for users. Here is a snippet of the code: <div class="col-12 p-0" v-for="( i, index ) in questions" :key="i"> <p cl ...

Positioning elements vertically and float them to the left side

I'm struggling to grasp the concept of the float attribute. Here's the code that is causing me confusion: #p1 { border: solid black 3px } <p id="p1" style="float:left">Paragraph 1</p> <a href="https://facebook.com" style="floa ...

Encountering an issue of duplicate key error when using multiple v-for loops with various keys

I've encountered an issue while working on a Python application that utilizes Vue.js. A ticket came my way with an error message stating: [Vue warn]: Duplicate keys detected: ''. This may cause an update error. (found in Root) The pro ...

Explore Youtube to find the most popular video IDs

Is it possible for me to use a string to search YouTube and retrieve the id for the top video in the search results? I want to be able to play that video directly on my website. All I have is the URL for the search: youtube_url/results?search_query=thevid ...

Display a popup notification when clicking in Angular 2

Can anyone help me with displaying a popup message when I click on the select button that says "you have selected this event"? I am using Angular 2. <button type="button" class="button event-buttons" [disabled]="!owned" style=""(click)="eventSet()"&g ...

Reload the Angular application and navigate to a different state

Introduction In my extensive angular application, there are numerous forms for various items. These forms are associated with action objects that have unique schemaforms. The dropdowns in these forms vary based on the specific action and its parent comp ...