PHP and .html() JQuery problem

It appears that .html() is somehow messing up reading the contents of a div.

Here is my php code which loads into the div.

$own_pre_selection .= "<input name='" . mysql_result($result,$i,"did") . "' value='" . mysql_result($result,$i,"did") . "' type='hidden'><span class='addremove' onclick=\\"recordchecks('" . mysql_result($result,$i,"dname") . "','" . mysql_result($result,$i,"did") . "')\\";><u>Remove</u></span> &nbsp;" . mysql_result($result,$i,"dname") . "<br>";

Here is what I see as source code after the page renders.

<input name='677' value='677' type='hidden'><span class='addremove' onclick="recordchecks('Heart Neoplasms','677')";><u>Remove</u></span> &nbsp;Heart Neoplasms<br><input name='1298' value='1298' type='hidden'><span class='addremove' onclick="recordchecks('Heart Septal Defects; Atrial','1298')";><u>Remove</u></span> &nbsp;Heart Septal Defects; Atrial<br></div>

When I test alert() the .html() contents it appears like this. It seems to screw up the quote escape and changes \" to " ;=“” after the recordchecks().

<input name="677" value="677" type="hidden"><span class="addremove" onclick="recordchecks('Heart Neoplasms','677')" ;=""><u>Remove</u></span> &nbsp;Heart Neoplasms<br><input name="1298" value="1298" type="hidden"><span class="addremove" onclick="recordchecks('Heart Septal Defects; Atrial','1298')" ;=""><u>Remove</u></span> &nbsp;Heart Septal Defects; Atrial<br>

So even though the source code looks perfect, if I alert() the div contents with html() it seems to get a bit garbled in the process. Any ideas?

In your php code

. "')\\";>

the semi-colon should be before the closing double quote that closes your onclick code.

. "')[B][COLOR="Red"];[/COLOR][/B]\\">

Validating your html on the w3c validator can greatly help find errors like this.

That is odd. No JavaScript or php errors are showing either because I assume that you would have mentioned them.

But aidos correction of you syntax seems like it should fix it. And if it does not then the proper syntax is not only proper but will prevent and solve any future issues you may have.

Here is where the problem is happening:

"onclick=\\"recordchecks('" .
mysql_result($result,$i,"dname") . "','" .
mysql_result($result,$i,"did") . "')\\";>"

You end up with the following attribute code:

<input ... onclick="recordchecks('...','...')";>

It’s that rogue semicolon that’s causing the trouble.

The web browser thinks that it’s another attribute, so the web browser changes the ; attribute to the more fully formed ;=“” attribute. It’s precise, but not accurate.

Solution: Either move the semicolon inside the doublequotes, or remove it completely.

Thank you for clearing that up! Make sense. Semi-colons— sometimes you love time and sometimes you can’t. I think its that coders are too trigger happy or Typer key happy to make sure that the ;'s are there.

When things aren’t working properly I always look at the html the php generates and I can usually then find the error and adjust the php code to fix it. If the error in the generated html is not obvious the w3c validator should detect the error. I ran the op’s html through the validator and it confirmed the semi-colon was the syntax error.

ahhh very interesting! thanks all!