如何在php变量中绑定html的正确语法

I am having a variable in php which contains an input with a inclick event like this:

$SelectButton = '<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, "id1")" value="Select All and Copy" /><br>';

I use this php variable in a javascript to prepend it to a class like this:

$('.surroundpre', this).prepend('$SelectButton');

When I look into the source with my browser, I see this code which is created:

<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, " id1")"="" value="Select All and Copy">

As you can see, the is a space before id1 and there is "="" before the value. So there must be something wrong with the syntax but I cannot figure out what is wrong

You need to use single quotes within the onclick attribute and escape them. Try this:

$SelectButton = '<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, \\\'id1\\\')" value="Select All and Copy" /><br>';