Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a jstl variable in javascript code. [duplicate]

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}
});
like image 371
webExplorer Avatar asked Jul 03 '14 16:07

webExplorer


1 Answers

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).

like image 136
Luiggi Mendoza Avatar answered Nov 15 '22 00:11

Luiggi Mendoza