How to pass template variable to javascript?

marring

Member
I need to use template variable in javascript, but I can not search the example.

For example:

Code:
<div>
    <xen:set var="$myServerTime">{$serverTime}</xen:set>
    <script type="text/javascript">
    <!--
    var currentTime = new Date($myServerTime)
    var month = currentTime.getMonth()
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write(day + "/" + month + "/" + year)
    //-->
    </script>
</div>

Thanks!
 
In your template:
HTML:
<div id="myUniqId"
    data-variable-1 = "$myVariable_1"
    data-variable-2 = "$myVariable_2"
    <xen:comment>myUniqId</xen:comment>
</div>

In your Javascript
Code:
var variable1 = $(#myUniqId).attr('data-variable-1'),
variable2 = $(#myUniqId).attr('data-variable-2');
 
  • Like
Reactions: KiF
Back
Top Bottom