Reputation: 7592
I am using php4 and jquery, I have got a php string variable $content which stores a string as shown below.
<html>
<head></head>
<table>
<tr>
<td>comments:</td>
<td>Good</td>
.....
...n rows
</table>
</html>
So, Now i want to change the color of all occurrences of text "Good" to red. So How to write a jquery function which takes a php string $content variable and changes the color of each "Good" word, (adds a style color:red) and returns it.
Upvotes: 0
Views: 837
Reputation: 164
You cannot access PHP variables in Javascript. Also, javascript works on the client side and PHP works on the server side.
I am unsure of the exact usage for this, but, here's my take on doing this:
// in javascript code
var x = "<?php echo $content; ?>"; // do take care of stripping quotes (")
x.replace("Good", "<span style='color:red'>Good</span>");
I guess that should do it.
Just to explain a bit more, on the server, PHP will dump the contents of the variable $content
to the variable x
in JS. On the client side, when JS is executed, it will pick this up and do the replacement.
Upvotes: 4
Reputation: 21617
$("td").each(function(){
$(this).html($(this).html().replace(/Good/, '<span style="color:#f00">Good</span>'));
});
This will look through each cell and find if there is a text value of "Good" and wrap a span around it
Upvotes: 1
Reputation: 173542
Pure PHP solution:
echo str_replace('Good', '<span style="color:red">Good</span>', $content);
Upvotes: 4
Reputation: 272
$content .= '<span class="red">';
$content .= {your existing code here}
$content .= '</span>';
After it you create a CSS Rule:
.red{ color:red; }
As you see you don't need jQuery at all.
Upvotes: 0