Bexie Bexie - 6 months ago 159
jQuery Question

Javascript Variable Function (Twig)

My default.htm (to set unique id)

{ % set uid = % }

<div id="tab" name="{{uid}}" class="my-tab draggable-tab ui-draggable">
<img id="tab-avatar" alt="Virtual agent avatar" style="height:50px; width:50px" src="{{__SELF__.avatarImage}}">
<div id="tab-minimize">
<div id="tab-label"></div>
<div id="tab-expand" name="{{uid}}" class="tab-expand tab-btn"></div>
<div id="tab-close" name="{{uid}}" class="tab-close tab-btn" onclick="tabClose()"></div>


function tabClose(){
var uid = document.getElementsByName("name");
$(this).click(function() {
$("#tab").addClass("hidden"); //<--- how to put the uid in?


var tab_{{uid}} = function tabClose(){
$("#tab-close").click(function() {

Can someone show me how to use twig in JavaScript?

How to set unique id for each component so that when I duplicate the component it will not have any errors?


You can't call server variable (twig) in client program (javascript).

You must write the twig variable in your html code for javascript catch it.

For example, tout can put it in data attribute.

<div id="tab-close" data-uniqid={{uid}} >

And in your js

var uid = $("#tab-close").data('uniqid');

or with your update

var uid = $("#tab").attr('name');


For your problem, I think You don't need Twig variable in your js, this variable is here to have uniq Id in your html code.

So, try this:

<div id="tab{{}}" class="my-tab draggable-tab ui-draggable">
    <img id="tab-avatar{{}}" alt="Virtual agent avatar" style="height:50px; width:50px" src="{{__SELF__.avatarImage}}">
    <div id="tab-minimize{{}}">
        <div id="tab-label{{}}"></div>
        <div id="tab-expand{{}}" class="tab-expand tab-btn"></div>
        <div id="tab-close{{}}" class="tab-close tab-btn" onclick="tabClose()"></div>

And in your js

 $(".tab-close").click(function() {