Pepe
Pepe

Reputation: 1002

How to insert a PHP variable in to HTML stored in a JS variable?

Is it possible to insert the ButtonSend var inside my <input> part? This way will not work:

var ButtonSend = <?php _e('Go!', $this->plugin_slug) ?>;
LangKeys['en']['button'] = '<input v-on:click.prevent="submit" type="button" value="ButtonSend" />';

Upvotes: 2

Views: 58

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

Assuming it's a string value (from the context of the property name) you need to wrap the PHP output in quotes. Then you can concatenate the value as you would any other:

var ButtonSend = '<?php _e('Go!', $this->plugin_slug) ?>';
LangKeys.en.button = '<input v-on:click.prevent="submit" type="button" value="' + ButtonSend + '" />';

Alternatively you could use a template literal to do the concatenation, assuming you don't need IE support:

LangKeys.en.button = '<input v-on:click.prevent="submit" type="button" value="${ButtonSend}" />';

Upvotes: 1

Related Questions