0

I have defined index as <%var index = 0;%> This is at the top of the body of HTML so I can use it throughout as a global variable. and I want to assign value of $(this).val() to <% index %>. I have tried multiple times but am not able to figure it out. I am using Node.js in backend and EJS in front end.

<script>
    $("#agency").on("change", function () {
        var $modal = $('#myModal');
            if($(this).val() !== '0'){
                <% index %> = parseInt($(this).val()); ????
            }
            $modal.modal('show');
        }
    });
</script>
5
  • 3
    It seems you're trying to mix server-side rendering and client-side rendering in a way that just won't quite work. As the script tag is executed after the page is loaded, it won't be able to properly parse the rendered EJS. Instead, consider adding a wrapper element around what you need to change, and updating that client-side instead of mixing server-side and client-side variables, since those won't be able to update in sync. Commented Mar 14, 2017 at 21:16
  • 2
    Your index variable is a server variable, while your script is running on the client. You cannot assign in the client environment something to a server variable. Check out Ajax to communicate data to the server. Commented Mar 14, 2017 at 21:16
  • use window.index=parseInt($(this).val()); to access it globally Commented Mar 14, 2017 at 21:17
  • ejs is not front end, this is where your mental picture breaks down. it's confusing because it's mixed together in front of you, but the ejs renders well before the js Commented Mar 14, 2017 at 21:18
  • @ajaiJothi That probably won't quite do what is expected. From what I can tell, the asker wishes to update a variable that will sync with another variable further up the page. In addition, I'm always pretty iffy on touching globals/window. Commented Mar 14, 2017 at 21:21

2 Answers 2

2

As someone mentioned you need to differentiate between what is calculated on the server and what is done on the browser:

A common page might look like:

<script>
    var index = <% index_calculated_on_server %>;
    $("#agency").on("change", function () {
        var $modal = $('#myModal');
            if($(this).val() !== '0'){
                index = parseInt($(this).val()); ????
            }
            $modal.modal('show');
        }
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem lies in mixing server-side and client-side rendering. To illustrate, let's take a look at the snippet you provided.

<script>
    $("#agency").on("change", function () {
        var $modal = $('#myModal');
            if($(this).val() !== '0'){
                <% index %> = parseInt($(this).val()); // This is the problem line
            }
            $modal.modal('show');
        }
    });
</script>

EJS is rendered before even being sent to the client, merging in variables provided to it, so if you set index to be zero, you'll end up with a snippet like this after the rendering is done.

<script>
    $("#agency").on("change", function () {
        var $modal = $('#myModal');
            if($(this).val() !== '0'){
                0 = parseInt($(this).val()); // Can't assign zero. 
            }
            $modal.modal('show');
        }
    });
</script>

Instead, what you should do is wrap your use of <% index %> in a wrapper element, which should allow you to manipulate it client side.

$("#agency").on("change", function () {
    var $modal = $('#myModal');
    if( $(this).val() !== '0' ) {
        $(".index-counter").text(parseInt($(this).val()));
    }
    $modal.modal = function () { /* stub function */ };
    $modal.modal('show');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" name="agency" id="agency" />

<!-- This would be where <% index %> was inserted -->
<div class="index-counter">0</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.