Saturday 15 May 2010

php - Jquery - change font color based on value of sql result -


I want to change the font color of a div using jquery where the div is populated by the output of the SQL query.

I have:

  $ (document) .ready (function () {$ ('# foo'). Each (function () {if ($ ( This) .text () == 'bar') {$ (this). CSS ('color', 'orange');}})}};   

A search that works fine when there is a text in the div.

But as it is SQL, I div with pop: ". $ Row ['Result']."

And it does not work anymore. I'm guessing this because SQL, though having a varchar field is $ variable and not like 'text'?

I'm sure this is something simple, but I'm struggling for this phrase in Google to return anything useful.

Many thanks.

Edit

The whole thing is quite long and before I was trying to add jquery, it was all working fine, so I add additional additions.

It is within the head:

  echo "& lt; script type = 'text / javascript' src = 'https: //ajax.googleapis.com/ajax /libs/jquery/1.4.4/jquery.js'> & lt; / scripts & gt; "; Echo "& lt; script type = 'text / javascript' & gt;"; Copy "$ (document) .ready (function () {$ ('# foo'). Each (function () {if ($ (this) .text () == 'bar') {$ (this) .css ('Orange color');}});});";   

Then I echo out a line in a loop:

$ sql = "SELECT ..."; $ $ Result = mysql_query ($ Sql) or die (mysql_error ());
($ line = mysql_fetch_array ($ result)) {code> echo "& lt; div id = 'foo' & gt;". $ Row ['Result & Lt; / Div & gt; ";
}

The entire document is wrapped in PHP but it is not a source of problem, such as I div to". $ Line ['Result'] 'can be included in the text instead. "Then it is implemented on jquery. It is ok.

You are giving the same ID (" foo "each div itemprop =" text ">

.each ( ) The function will be called on only one element, possibly before.

Change the HTML output in this way: echo "

  $ ('.foo'). Function () {// ...}    

No comments:

Post a Comment