Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:46:09+00:00 2026-06-04T19:46:09+00:00

I need to use a Javascript variable (defined in the front-end) in some EJS

  • 0

I need to use a Javascript variable (defined in the front-end) in some EJS code, as follows:

var selected = 1;
<% for (var i=0; i < supplies.length; i++) { %>
    if (i == selected) {
        console.log(supplies);    
    }
<% } %>

I’m using EJS, Express.js and socket.io. I could convert the Javascript variable to an EJS variable by sending a message to my Node.js server instance, but that’s kind of silly… Is there a way to use Javascript variables inside EJS?

EDIT:
I want to access supplies, a javascript array, after the user selected an item from a drop down menu. When he selects this item, a javascript function with the above code needs to access some EJS. That’s why I need to use a normal Javascript variable in EJS.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:46:11+00:00Added an answer on June 4, 2026 at 7:46 pm

    Can I pass a JavaScript variable into a template?:

    It is possible to get more data into the template and re-render it, but not in the manner that you’re thinking, and without making another request to the server (unless it’s to get more data, not to get more HTML).

    Solution:

    This question is going to be difficult to answer without more details, so I’m going to make some assumptions about why you want to pass a selected value into an EJS template. I’ll do my best to answer this with limited information about your goals.

    It seems like your user is performing some action on the page, like selecting a cleaning supply, and you want to render the data differently, based on which element the user selected. To do this, you can re-render the template and pass in data that identifies which element is selected, using a view helper to apply a specific class to the selected element:

    Here is the modified cleaning.ejs template, with the class added using the view helper:

    cleaning.ejs:

    <script>
    // NOTE: even if uncommented, JavaScript inside a template will not run.
    //    var selected = 1;
    </script>
    <h1><%= title %></h1>
    <ul>
        <% for(var i=0; i<supplies.length; i++) { %>
    
            <li>
                <!-- apply a class to the selected element -->
                <a class='<%= supplies[i].selected %>' href='supplies/<%= supplies[i].value %>'>
                    <%= supplies[i].value %>
                </a>
            </li>
        <% } %>
    </ul>    
    

    The rendered HTML looks like this:

    <script>
        /** NOTE: Script blocks will not fire in rendered templates. They are ignored
        //    var selected = 1;
    </script>
    <h1>Cleaning Supplies</h1>
    <ul>
    
    
            <li>
                <a class="" href="supplies/Broom">
                    Broom
                </a>
            </li>
    
    
            <li>
                <!-- Note the selected element -->
                <a class="selected" href="supplies/mop">
                    mop
                </a>
            </li>
    
    
            <li>
                <a class="" href="supplies/Hammer">
                    Hammer
                </a>
            </li>
    
    </ul>
    

    This view was rendered using the following JavaScript code:

    // modified data structure so that array of supplies contains objects
     // with the name of the item and whether or not it's selected.
    data = {
        "title":"Cleaning Supplies",
        "supplies":[
            {
                "value":"Broom",
                "selected":""
            },
            {
                "value":"mop",
                "selected":"selected"
            },
            {
                "value":"Hammer",
                "selected":""
            }
        ]
    };
    
    // pass data into template and render
    var html = new EJS({url: 'cleaning.ejs'}).render(data);
    
    // add HTML to the DOM using a <div id="container"></div> wrapper.
    document.getElementById("container").innerHTML = html;
    

    As you can see, supplies[i].selected applies the selected class to the element that was marked as selected in the data structure. I modified the href value so that it accessed the object in the ith array item instead of the value of the array itself.

    Now, when the selected item is modified, we simply modify the data variable, re-render the EJS template, and add it to the DOM.

    With this CSS in the head of your HTML document, you’ll see something similar to what’s displayed below:

    <style>
        .selected { color:red; }
    </style>
    

    Demo of selected element in red


    Why JavaScript in the template doesn’t run:

    The method that you’re attempting to use to manipulate JavaScript values or use JavaScript values inside the EJS template won’t work. This has to do mainly with the context of when the JavaScript is executed.

    You’re right to think that the EJS templates are compiled on the client-side. However, the JavaScript in the view helpers are executed independent of the JavaScript in the Global context. From looking at the ejs.js source code, it appears as if eval is used in the process.

    Additionally, EJS returns the rendered HTML as a string, and the documentation for EJS instructs us to inject that rendered template string into the DOM using innerHTML:

    document.getElementById("container").innerHTML = html;
    

    No matter what view technology you’re using, one of the fundamental truths of some browsers when it comes to JavaScript is this: Some browsers may not evaluate <script> blocks added to the DOM using innerHTML.

    In other words, in my test template, when I tried adding a script tag to output the selected value to the console, I could see that the script block was added, but due to the way innerHTML works, it was not executed:

    Example Template Demonstrates JavaScript won’t run if added using innerHTML:

     <h1><%= title %></h1>
    <ul>
        <% for(var i=0; i<supplies.length; i++) {  %>
           <span id="selected"></span><script>console.info('test <%= i %> = <%= supplies[i] %>');</script>            
            <li>
                <a href='supplies/<%= supplies[i] %>'>
                    <%= supplies[i] %>
                </a>
            </li>
        <% } %>
    </ul>
    

    Rendered HTML:

    As you can see below, the console.log statements are present in the HTML. However, when added using innerHTML, they will not fire.

    The approach to take with view technologies is to limit their use to just that, rendering the view. Keep your logic in the “regular JavaScript”.

    <h1>Cleaning Supplies</h1>
    <ul>
    
           <span id="selected"></span><script>console.info('test 0 = Brrom');</script>            
            <li>
                <a href='supplies/Brrom'>
                    Brrom
                </a>
            </li>
    
           <span id="selected"></span><script>console.info('test 1 = mop');</script>            
            <li>
                <a href='supplies/mop'>
                    mop
                </a>
            </li>
    
           <span id="selected"></span><script>console.info('test 2 = Hammer');</script>            
            <li>
                <a href='supplies/Hammer'>
                    Hammer
                </a>
            </li>
    
    </ul>
    

    More examples and documentation can be found on EJS Templates on the Google Code Embedded JavaScript site.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use some rails code withing a javascript and need to
I need to use a code in JavaScript to determine a user, I decided
I'm using blogger.com. I need to use data:blog.url variable on the javascript. But i
how can I run ruby code inside javascript in haml? if I use var
Is there some window variable in javascript that I can use or a way
I need to use basic javascript here and I'm not well versed with it.
I need to use Substring in Javascript. But despite my searches, i have an
I have a javascript which I didn't write but I need to use it
I have a large javascript which I didn't write but I need to use
I need to return an array in php and use it in javascript. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.