jQuery find td with class name and change the text

14,173

Solution 1

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

Solution 2

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>

Share:
14,173

Related videos on Youtube

Andry
Author by

Andry

Updated on June 04, 2022

Comments

  • Andry
    Andry almost 2 years

    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");
                }
            }
        );
    })();
    
  • Andry
    Andry about 5 years
    Can u look at tampermoney script please. I changed that way and still nothing.
  • Andry
    Andry about 5 years
    Added an example of working alarm using this script
  • Andry
    Andry about 5 years
    no. Maybe i should print something in console for better understanding a problem.
  • Andry
    Andry about 5 years
    Changed quotes and added as u said. Still nothing.
  • Maneren
    Maneren about 5 years
    And in which moment do you execute your code? Is it after properly loading HTML? Like you did in alert example. Otherwise wrap this code in window.onload or similar.
  • Andry
    Andry about 5 years
    Actually, i have got this alarm before page loads properly. Should i use setTimeout then?
  • Maneren
    Maneren about 5 years
    Better than setTimeout wil be $(document).ready( "that piece of code" )