0

I need to append a DOM element to another using javascript or jQuery, and this DOM element has to contain a translatable string using PHP like so:

<?php echo $this->__("submit"); ?>

I tried something like:

var string = '<?php echo $this->__("submit"); ?>';
jQuery('.my-element').append('<span>' + string + '</span>');

But the php part is rendered as plain text. Is there a way to accomplish something like that?

Zac
  • 121
  • 2
  • 10
  • 2
    PHP runs on the server, not the client. – Pointy Oct 24 '18 at 13:19
  • Should there be a quote and close bracket in `';`? – Nigel Ren Oct 24 '18 at 13:20
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – CD001 Oct 24 '18 at 13:20
  • So long as you execute that JS logic within a `.php` page, it will work. If you place it in a `.js` file it won't as the PHP won't be interpreted. The alternatives would be to output the PHP to a hidden element in the DOM, then clone it when needed, or to make an AJAX request and return the required HTML from your PHP code on the server. – Rory McCrossan Oct 24 '18 at 13:21
  • *«But the php part is rendered as plain text. »* --> Yes... How else do you expect it to be rendered? – Louys Patrice Bessette Oct 24 '18 at 13:27
  • I just needed to try and see i guess :) – Zac Oct 24 '18 at 13:28

0 Answers0