how can I access a javascript variable in my HTL attribute | Community
Skip to main content
New Participant
July 20, 2022
Solved

how can I access a javascript variable in my HTL attribute

  • July 20, 2022
  • 3 replies
  • 1505 views
<sly data-sly-test=${isTrue}>
                <input type="radio" id="north-america" name="regions" value="north-america" class="destaco-input-radio">
                <label for="north-america">${"America" @ i18n, context='html'}</label>
</sly>
<script>
    var isTrue= true;
</script>
 
 
 
I am confused about how to use the "isTrue" variable inside my data-sly-test statement so that I can execute this piece of code
Thank you
 
 
I have to use this variable for both "true" and "false" statements
Please help me find out how to do it .
Thank you
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

Hi,

You cannot use client side javascript variables value for server side execution.

You can always execute the code and based on javascript condition show/hide or do business logic on client side.

3 replies

Mohit_KBansal
Employee
July 20, 2022

As mentioned by Arun, you can not use clientSide variables to render server side scripts.

 

In this case, you render your HTML and use javascript to show/hide block based on js Variable value.

<div class="showInput">
    <input type="radio" id="north-america" name="regions" value="north-america" class="destaco-input-radio">
    <label for="north-america">${"America" @ i18n, context='html'}</label>
</div>

# Hide showInput div block on page load
<style type="text/css">
    .showInput {
        display: none;
    }
</style>

<script>
    if(isTrue) {
        // Display showInput div block if JS object is true
    }
</script>
arunpatidar
arunpatidarAccepted solution
New Participant
July 20, 2022

Hi,

You cannot use client side javascript variables value for server side execution.

You can always execute the code and based on javascript condition show/hide or do business logic on client side.

Arun Patidar
Employee
July 20, 2022