Andry
Andry

Reputation: 49

jQuery find td with class name and change the text

I'm trying to use jquery to find a text based on it's class name and change it. Basically I have this structure:

<td class="walletBalance">0.0000 XBT</td>

So, im trying:

$("tbody").find("tr").each(function() { //get all rows in table
var ratingTd = $(this).find('td.walletBalance’);//Refers to TD element
if (ratingTd.text() == “0.0000 XBT”) {
    ratingTd.text(’changed text’);
}
});

What im doing wrong?

For more understanding html structure

Specifically what value i'm trying to change

PS: Im using tampermonkey

    // ==UserScript==
// @name         testingalarm
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.hey
// @grant        none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    $("tbody").find("tr").each(function() {
  var ratingTd = $(this).find('td.walletBalance');
  if (ratingTd.text() == "0.0000 XBT") {
    ratingTd.text('changed text');
  }
});
})();

Btw, this alarm is working:

// ==UserScript==
// @name         testingalarm
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.some
// @grant        none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    $(document).ready(function() {
  alert('WINNING');
});
})();

PSS: after Manaren answer

// ==UserScript==
// @name         aaaaa
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.some
// @grant        none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    $("tbody tr td.walletBalance").each(
        function(){
            if ($(this).text() == "0.0000 XBT"){
                $(this).text("changed text");
            }
        }
    );
})();

Upvotes: 2

Views: 24650

Answers (2)

Maneren
Maneren

Reputation: 128

I would try this, i tested it in browser and it works correctly.

$("tbody tr td.walletBalance").each(
        function(){
            if ($(this).text() == "0.0000 XBT"){
                $(this).text("changed text"); 
            }
        }
    );

Issues:

  • Wrong quotes characters

  • Imo too complex code for so simple task

  • Maybe some issues with import

Upvotes: 4

Rory McCrossan
Rory McCrossan

Reputation: 337560

The issue is because you're using invalid single and double quote characters. Single quotes should be ', not , and double quotes need to be ", not or . Once that is fixed your code works fine:

$("tbody").find("tr").each(function() {
  var ratingTd = $(this).find('td.walletBalance');
  if (ratingTd.text() == "0.0000 XBT") {
    ratingTd.text('changed text');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td class="walletBalance">0.0000 XBT</td>
  </tr>
</table>

Upvotes: 1

Related Questions