enjoimark enjoimark - 2 months ago 7
Javascript Question

jquery find abbrev title in table td

I have been using the following for a while to find text in a TD, and add an image to it... but some of the usernames (text in the td) are too long, so they are cutoff (12 character limit). What I need to do is find the abbrev title, which would be the full text of my winner variable, instead of just text.

var winner = "<?php echo $winner; ?>";
if (winner == 0){
} else {
$("td:contains('<?php echo $winner; ?>')").each(function(){
$(this).append('<br><br><img src="images/trophy/gold.png" style="width: 40px; margin-left: 30px">');
});
};

Answer

Update your code to consider only the first 12 char of the php variable:

var winner = "<?php echo $winner; ?>";
var winner_short = "<?php echo substr($winner,0,12); ?>";
if (winner == 0){
} else {
    $("td:contains('"+winner_short+"')").each(function(){
        $(this).append('<br><br><img src="images/trophy/gold.png" style="width: 40px; margin-left: 30px">');
    });
};

If this doesn't work you can also do:

var winner = "<?php echo $winner; ?>";
if (winner == 0){
} else {
    $("td:contains('<?php echo substr($winner,0,12); ?>')").each(function(){
        $(this).append('<br><br><img src="images/trophy/gold.png" style="width: 40px; margin-left: 30px">');
    });
};