Update the value of a table cell with jQuery

I need assistance with changing the value of a td when a specific button is clicked. I have attempted various methods but none seem to be working. Ideally, I want the column to display only USD values when the "Show USD" button is clicked, and display only GBP values when the "GBP" button is clicked. I'm unsure if my current approach is correct or not. Any help would be greatly appreciated.

$('.btn-usd').on('click', function(){
    $("cu-usd").removeClass(hide);
    $("cu-gbp").addClass(hide);
});
$('.btn-gbp').on('click', function(){
    $("cu-gbp").removeClass(hide);
    $("cu-usd").addClass(hide);
});
.hide {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-usd">show USD</div>
<div class="btn-gbp">show GBP</div>
<table>
    <tbody>
        <tr> 
            <td>
                <div class="cu-usd">$10</div>
                <div class="cu-gbp">£7.10</div>
            </td>
            <td>
                <div class="cu-usd">$20</div>
                <div class="cu-gbp">£14.20</div>
            </td>
            <td>
                <div class="cu-usd">$30</div>
                <div class="cu-gbp">£21.30</div>
            </td>
            <td>
                <div class="cu-usd">$40</div>
                <div class="cu-gbp">£28.10</div>
            </td>
        </tr>
    </tbody>
</table>
<table>
    <tbody>
        <tr> 
            <td>
                <div class="cu-usd">$100</div>
                <div class="cu-gbp">£70.10</div>
            </td>
            <td>
                <div class="cu-usd">$200</div>
                <div class="cu-gbp">£140.20</div>
            </td>
            <td>
                <div class="cu-usd">$300</div>
                <div class="cu-gbp">£210.30</div>
            </td>
            <td>
                <div class="cu-usd">$400</div>
                <div class="cu-gbp">£280.10</div>
            </td>
        </tr>
    </tbody>
</table>

Answer №1

Two issues have been identified

  1. The class selector begins with a dot (.)
  2. "hide" is a string that must be enclosed in quotes

The corrected code snippet is shown below:

$('.btn-usd').on('click', function(){
        $(".cu-usd").removeClass("hide");
        $(".cu-gbp").addClass("hide");
    });
    $('.btn-gbp').on('click', function(){
        $(".cu-gbp").removeClass("hide");
        $(".cu-usd").addClass("hide");
    });

Answer №2

$(".usd-currency").removeClass('hide');
$(".gbp-currency").addClass('hide');
$('.btn-usd').on('click', function(){
    $(".usd-currency").removeClass('hide');
    $(".gbp-currency").addClass('hide');
    });
    $('.btn-gbp').on('click', function(){
    $(".gbp-currency").removeClass('hide');
    $(".usd-currency").addClass('hide');
    });
.hide {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-usd">show USD</div>
    <div class="btn-gbp">show GBP</div>
    
    
    <table>
    <tbody>
    <tr> 
    <td><div class="usd-currency">$10</div><div class="gbp-currency">£7.10</div></td>
    <td><div class="usd-currency">$20</div><div class="gbp-currency">£14.20</div></td>
    <td><div class="usd-currency">$30</div><div class="gbp-currency">£21.30</div></td>
    <td><div class="usd-currency">$40</div><div class="gbp-currency">£28.10</div></td>
    </tr>
    </tbody>
    </table>
    <table>
    <tbody>
    <tr> 
    <td><div class="usd-currency">$100</div><div class="gbp-currency">£70.10</div></td>
    <td><div class="usd-currency">$200</div><div class="gbp-currency">£140.20</div></td>
    <td><div class="usd-currency">$300</div><div class="gbp-currency">£210.30</div></td>
    <td><div class="usd-currency">$400</div><div class="gbp-currency">£280.10</div></td>
    </tr>
    </tbody>
    </table>

Answer №3

$('.btn-usd').on('click', function() {

  $(".cu-usd").removeClass('hide');//fixing missing .
  $(".cu-gbp").addClass('hide');//fixing missing .
});
$('.btn-gbp').on('click', function() {
  $(".cu-gbp").removeClass('hide');//fixing missing .
  $(".cu-usd").addClass('hide');//fixing missing .
});
.hide {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-usd">show USD</div>
<div class="btn-gbp">show GBP</div>


<table>
  <tbody>
    <tr>
      <td>
        <div class="cu-usd">$10</div>
        <div class="cu-gbp">£7.10</div>
      </td>
      <td>
        <div class="cu-usd">$20</div>
        <div class="cu-gbp">£14.20</div>
      </td>
      <td>
        <div class="cu-usd">$30</div>
        <div class="cu-gbp">£21.30</div>
      </td>
      <td>
        <div class="cu-usd">$40</div>
        <div class="cu-gbp">£28.10</div>
      </td>
    </tr>
  </tbody>
</table>
<table>
  <tbody>
    <tr>
      <td>
        <div class="cu-usd">$100</div>
        <div class="cu-gbp">£70.10</div>
      </td>
      <td>
        <div class="cu-usd">$200</div>
        <div class="cu-gbp">£140.20</div>
      </td>
      <td>
        <div class="cu-usd">$300</div>
        <div class="cu-gbp">£210.30</div>
      </td>
      <td>
        <div class="cu-usd">$400</div>
        <div class="cu-gbp">£280.10</div>
      </td>
    </tr>
  </tbody>
</table>

Corrected . in class name

Answer №4

Your code has a few issues that need to be addressed.

$('.btn-usd').on('click', function(){
  $(".cu-usd").removeClass('hide');
  $(".cu-gbp").addClass('hide');
});
$('.btn-gbp').on('click', function(){
 $(".cu-gbp").removeClass('hide');
 $(".cu-usd").addClass('hide');
});

Remember to use the . operator for class elements and wrap 'hide' in quotes as it is passed as a string.

For optimization purposes, consider storing $(".cu-usd") and $(".cu-gbp") in variables.

Answer №5

You have the option to utilize .hide() and .show() without needing to assign a class, resulting in the same outcome.

It was previously noted that there is a missing . in the class name. However, it is assumed that you are already aware of this by now.

 $('.btn-usd').on('click', function(){
        $(".cu-usd").show();
        $(".cu-gbp").hide();
       });
    $('.btn-gbp').on('click', function(){
        $(".cu-gbp").show();
        $(".cu-usd").hide();
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-usd">show USD</div>
<div class="btn-gbp">show GBP</div>
    <table>
    <tbody>
    <tr> 
    <td><div class="cu-usd">$10</div><div class="cu-gbp">£7.10</div></td>
    <td><div class="cu-usd">$20</div><div class="cu-gbp">£14.20</div></td>
    <td><div class="cu-usd">$30</div><div class="cu-gbp">£21.30</div></td>
    <td><div class="cu-usd">$40</div><div class="cu-gbp">£28.10</div></td>
    </tr>
    </tbody>
    </table>
    <table>
    <tbody>
    <tr> 
    <td><div class="cu-usd">$100</div><div class="cu-gbp">£70.10</div></td>
    <td><div class="cu-usd">$200</div><div class="cu-gbp">£140.20</div></td>
    <td><div class="cu-usd">$300</div><div class="cu-gbp">£210.30</div></td>
    <td><div class="cu-usd">$400</div><div class="cu-gbp">£280.10</div></td>
    </tr>
    </tbody>
    </table>

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

Creating a three-dimensional representation by projecting onto the surface of a sphere in ThreeJS

3D animation is a realm filled with numerous terms and concepts that are unfamiliar to me. I am particularly confused about the significance of "UV" in 3D rendering and the tools used for mapping pixels onto a mesh. I have an image captured by a 360-degre ...

Page Not Found: React and Express Route Not Found

I encountered an error while attempting to use the sign-up route: POST http://localhost:3001/sign-up 404 (Not Found). I searched for similar issues on StackOverflow but couldn't pinpoint the source of my problem... In the frontend, I am fetching the ...

Is it possible for me to save external CDN JavaScript files to my local system?

Normally, I would include scripts from other providers in my application like this: <script src="https://apis.google.com/js/api.js"></script> However, I am considering whether it is feasible to simply open the URL , and then copy and paste th ...

Steps for isolating AXIOS requests in a Vuex store

My Vuex store file appears very typical and contains the following code: //store.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); export const store = new Vuex.Store({ state: { loading: true, companyBasicI ...

Is it possible to load the response from the $.post function, which contains GIF content, into a JavaScript Image object

How can I preload an image that the server only returns on a POST request? I want to load the image before displaying it. How can I store the result in a JavaScript object? $.post('image.php', {params: complexParamsObject}, function(result) { ...

Having trouble getting CSS media query to work in my Rails mailer template

Is it true that media queries do not work in email templates? I have a simple test template and a query BUT can't seem to get it to function properly. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DT ...

What is the reason for the find() method not displaying the most recent data from a MongoDB database in an Express.js application?

Upon calling the app.post('/form-submit', funtion(req, res)) method, my expectation is for it to first save the data using save(). This works fine, but then when I call the find() method, it shows all the data from the mongoDB database except for ...

As the screen size shrinks, two components merge together

One issue I'm facing with my site is the component called NavPanel. This consists of two components - a back button (BackToButton) and a search field (SearchTextField). Everything looks fine on a standard screen size, but when the screen size decrease ...

Steer clear of altering line spacing in CSS

Here is the HTML structure that I am working with: <div id="id1"> <h1>my name </h1><h3><a href="mailto:myemail_id"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff92869a929e9693969bbf878685d19 ...

What is the best way to retrieve error messages from a transaction with ethers.js?

Incorporating a custom error in my Solidity contract: error Documents__AlreadyExist(string hash); The modifier I utilize is as follows: modifier alreadyExist(string memory hash) { if (s_documentsHashes[hash]) { revert Documents__AlreadyExi ...

Choose all checkboxes that have a certain identifier

Currently, I am developing a menu permission project using vue.js. Within this project, I have various submenus that are children of different menus. My objective is to automatically select all submenus under a selected menu when the user clicks on "select ...

The data retrieved from the $.ajax() request in Vue.js is not properly syncing with the table

After setting up an $.ajax() function and ensuring the data binding is correctly configured, I expected the data to append to a table on page load without any issues. However, the data is not appearing as expected. Is there something that I might be overlo ...

Incorporating jQuery to Load Content into a DIV while preserving the original JavaScript

I am attempting to implement the following <script> $(document).ready( function() { var data = 'testing' $("#about").on("click", function() { $("#main-content").load("/about.html"); ...

Troubleshooting: Missing Headers in AngularJS HTTP Requests

When I make a request using AngularJS like this: var config = { headers: { 'Authorization': 'somehash', 'Content-Type': 'application/json; charset=UTF-8' } }; $http.get('htt ...

When utilizing the $.ajax method with JsonResult, the response will contain the following code snippet: "toJSON - function (key) { return this.valueOf(); }

Here is the code snippet: Coding Section [HttpPost] public ActionResult GetNumbers(int id) { List<int> numbers = new List<int>(); //The desired numbers! numbers.Add(4); numbers.Add(8); retur ...

Excessive content spilling over into the footer area due to an integrated forum feature within

We have integrated a Muut forum into our website, and it is working well when embedded in a coding block within the page's body. However, there is an issue where the sidebar of the forum gains extra features when logged in as an admin. This causes the ...

Leveraging Firebase Dynamic Links through JavaScript

Currently exploring options for implementing Dynamic Links. Firebase Dynamic Links seem promising, but the lack of support for Cordova/ ionic apps is concerning. Is there any plan to add this in the future? Are there any other alternatives that you would ...

Placing a JavaScript button directly below the content it interacts with

I am currently working on a button that expands a div and displays content upon clicking. I am facing an issue where I want the button to always be positioned at the bottom of the div, instead of at the top as it is now, but moving it within the parent div ...

Perform the cloning process for each item listed in my JSON file

I'm trying to display a list of names from a JSON file on my webpage, each in their own separate div. However, I've been unsuccessful in getting it to work so far. Currently, this is what I have attempted: var cloneTeam = $('.team').c ...

Encountering an "AJAX not a function" error while using the d3/flask interface

Hey there! I'm new to the world of JavaScript and AJAX. Take a look at this d3 function I've been working on: var node = g.selectAll(".node") .data(root.descendants()) .enter().append("g") .attr("class", function(d) { return "node" + ...