Reading a jstl variable in javascript code.

java, javascript, jquery, jstl

Solution

Just write the Expression Language directly in your JavaScript code:

$("#textBoxInp").keyup(function() {
    var userId = '${userId}';
});

Note that this won't work if the JavaScript code is placed in a external file and is invoked in the JSP. In this case, you may refer to one of the four ways that BalusC explain here: Mixing JSF EL in a Javascript file (he explains five, but one of them is JSF specific).

Problem

I want to read a jstl variable in a javascript function. JS code submits a form. ``` $("#userSubmit").on('submit', function () { document.getElementById("userForm").submit(); }); ``` So in server code - ``` request.setAttribute("userId", 435); ``` and after the page is loaded -> in the javascript code - ``` $("#textBoxInp").keyup(function() { // I want to access the userId here. // in html code i can acccess it using JSTL syntax ${userId} }); ```

Original source

Related problems