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>
Related videos on Youtube
Author by
Andry
Updated on June 04, 2022Comments
-
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 about 5 yearsCan u look at tampermoney script please. I changed that way and still nothing.
-
Andry about 5 yearsAdded an example of working alarm using this script
-
Andry about 5 yearsno. Maybe i should print something in console for better understanding a problem.
-
Andry about 5 yearsChanged quotes and added as u said. Still nothing.
-
Maneren about 5 yearsAnd 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 about 5 yearsActually, i have got this alarm before page loads properly. Should i use setTimeout then?
-
Maneren about 5 yearsBetter than setTimeout wil be $(document).ready( "that piece of code" )