javascriptphpjquerymarkupinline-scripting

How to print a line of HTML markup containing inline javascript and nested quoting?


How should I quote this:

<tr onclick="$.colorbox({href:'information1.html'});">

When put in an echo " "; ?

I have tried this:

echo "<tr onclick='$.colorbox({href:'information1.html'});'>";

Which shows a Jquery error.

And I tried this:

echo "<tr onclick="$.colorbox({href:'information1.html'});">";

Which shows a PHP error.


Solution

  • You need to escape the quotes symbols:

    echo '<tr onclick="$.colorbox({href:\"information1.html\"});">'
    

    Note that using inline script is not considered to be a good practice!

    echo '<tr class="foo">'
    

    In the javascript code:

    $('.foo').click(function() {
        $.colorbox({ href: "information1.html" });
    });​