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>
window.index=parseInt($(this).val());to access it globally