Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change table value using jquery

I am trying to change the value of an td value with the button that is clicked.. I have tried a couple of ways but none have worked. If user click Show USD button column show only USD values, If user click GBP column should show GBP values. I don't know this this is correct way to do this. Any help highly 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>
like image 274
techies Avatar asked Jun 22 '16 11:06

techies


People also ask

How to change the value of an attribute in jQuery?

The jQuery attr () method is also used to set/change attribute values. The following example demonstrates how to change (set) the value of the href attribute in a link: The attr () method also allows you to set multiple attributes at the same time.

How to edit a row in a table using jQuery?

Also, you can click the "Edit" link under the "Option" column and the whole row will become editable. And edit link will be replace with "Save | Cancel |". Let's dissect this whole editable process so you can use it in your own web projects. Let's assume that you got your table data via ajax call in jquery and it has the following array:

What is jQuery DataTable?

Introduction to jQuery Data Table jQuery DataTable is a powerful and smart HTML table enhancing plugin provided by jQuery JavaScript library.

How to set the value of input element in jQuery?

There is a Input Element, the task is to set its value using JQuery. Here are a few examples discussed. To understand example first few methods to know. JQuery val() method: This method return/set the value attribute of selected elements. If we use this method to return value, it will return the value of the FIRST selected element.


1 Answers

There are 2 problems

  1. Class selector starts with .
  2. hide is a string must be in quotes

Following will work

 $('.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");
    });
like image 155
Nikhil Aggarwal Avatar answered Oct 05 '22 01:10

Nikhil Aggarwal