How to use expression language (EL) in JavaScript / jQuery function?

How do you get a value from Expression Language (EL) in JSP for a JavaScript function?

On my JSP page, I have:

<li>${state}</li> <input title="Show ${state}">Show</> 

And I would like to get ${state} from JSP into a JavaScript function:

 $('#input').click(function() { if ($(this).val() == "Show + ${state}">") { $(this).val("Show"); $(this).attr("title", "Hide + ${state}"> "); } else { $(this).val("Hide"); $(this).attr("title", "Hide + ${state}">"); } }); 

I want the name of each button to show Ohio or Hide Ohio shows, but to change any state in the <li> .

+4
source share
2 answers

In order for JSP to embed EL variables, JavaScript must go to <script> inside the .jsp file instead of the .js file.

Alternatively, you can also just do a simple line-replacement of the first word or a substring of the first four characters. This way you don't need EL in JavaScript, and you can just put the JS code in your own .js file.

By the way, the logical flow of your entire function makes its own very little meaning. Would you like to check if val() "Hide" ? Don't you want to use "Show" in the name of one of two conditions?

+1
source

If your possible values ​​are Show ${State} and Hide ${State} , then you only need the first four characters:

 if ($(this).val().substring(0, 4) == "Show") { ... } ... 
0
source

All Articles