RE: Passing String Parameter to javascript function in jsp is not working

Jamie Sammons, modified 3 Years ago. New Member Posts: 3 Join Date: 4/3/22 Recent Posts

I have one JSP file This file contains some below code.

<%
    String data = "some data from database";
%>
<aui:input type="checkbox" name="isOption" label="isOption"  onChange="javascript:onChangeCheckBox('<%= data %>');"></aui:input>
  
<script>
function onChangeCheckBox(input)
{
    console.log("Input------------>"+input);
}
</script>


  
 
When I have a checking or unchecked the checkbox from UI it will call the onChangeCheckBox function and print the input on the console.

But instead of printing the actual string, it will be printing the below output on the console.

Input------------><%= data %>
  

thumbnail
Olaf Kock, modified 3 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts

If memory serves me right, having scriptlet in tags requires the whole value to be a scriptlet.

E.g. instead of

<some:thing key="some <%="value"%>"/>

you'd need

<some:thing key="<%="some " + "value"%>"/>

or similar

Jamie Sammons, modified 3 Years ago. New Member Posts: 3 Join Date: 4/3/22 Recent Posts

Thanks for your answer issue is now resolved

thumbnail
Jamie Sammons, modified 3 Years ago. Junior Member Posts: 39 Join Date: 3/9/22 Recent Posts

Hello, replace this

<% String data = "some data from database"; %>

javascript:onChangeCheckBox('<%= data %>')

with

${data = "some data from database"}

javascript:onChangeCheckBox('${data}')