0

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.

1
  • Have you read any of the Related questions yet? Commented Apr 18, 2012 at 6:23

4 Answers 4

4

Pure PHP solution:

echo str_replace('Good', '<span style="color:red">Good</span>', $content);
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1
$("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

Comments

0

$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.