Jordyn Jordyn - 23 days ago 9
Javascript Question

Adding a jQuery code to a PHP string

I want to add a jQuery code to a PHP string

Here is the PHP code

$my_code ='
<script src="js/jquery.form.js"></script>
<script>
$(document).ready(function()
{
$("#my_form").on("submit", function(e)
{
e.preventDefault();
$("#myButton").attr("disabled", "");
$("#my-output").html(‘<div class="alert alert-info" role="alert">My message</div>’);
$(this).ajaxSubmit({
target: "# my-output ",
success: afterSuccess
});
});
});

function afterSuccess()
{

$("#myButton").removeAttr("disabled");

}
</script>
‘;


Issue I’m having in this code line

$("#my-output").html(‘<div class="alert alert-info" role="alert">My message</div>’);


I even try change the single quote to double quotes and when I do code stopped working. It works fine if I don’t have a div inside above code line.
Ex:

$("#my-output").html(“My message”);


Appreciate your answers.

Answer

You can use single quotes inside the string, however you will need to \' escape them:

'$("#my-output").html(\'<div class="alert alert-info" role="alert">My message</div>\');'