user2417332
user2417332

Reputation: 59

Get_option() in JavaScript error?

When i star my page i receive this error in firebug: "SyntaxError: unterminated string literal". It is my code:

$('.survey_title').html('<?php 
                    $found1 = get_option("heading");
                    if($found1)
                    {
                        echo  $found1 ;

                    }
                ?>').css('color', 'red');

Where is the problem???

Upvotes: 0

Views: 157

Answers (1)

Brad
Brad

Reputation: 163324

In JavaScript, you need to use a backslash \ before a new line in a string literal.

What you should be doing is using json_encode() around any value from PHP to JavaScript to let it fix the escaping for you. Better yet, to assign it to a variable before your JS to make it easier to read.

var titleHTML = <?php echo json_encode($found1); ?>;
$('.survey_title').html(titleHTML).css('color', 'red');

Upvotes: 3

Related Questions