3

I need to read a variable and then have to pass that as an argument to a function after onclick(). I am using JSP, so I am reading the variable using scriptlets

<% int lateRegDays = 6;%>

I am passing the variable as

onclick="displayDatePicker('startTestAdminNo','mdy',<%=lateRegdays %>,120,0,3);"

But, I am unable to pass the valueof 6. The string "<%=lateRegDays%>" is being passed to the function. However, when I tried to print the value using alert, it worked. The changes I did is,

onclick='<%="alert("+lateRegDays+")"%>'

In the similar fashion, I need to know, how to pass all my arguments to function with this scriptlet variable. The entire code snippet is:

<td><input name="startTestAdminNo"></td>
  <td>
        <chtml:img srcKey="order.calendar.search" onclick="displayDatePicker('startTestAdminNo','mdy',<%=lateRegdays %>,120,0,3);"
            altKey="order.calendar.alt_search" bundle="prompt" />
  </td>

I need help with this as I am stuckand I don't know how to pass multiple arguments along with a scriptlet variable

1 Answer 1

2

Scriplets will generate the HTML content when the Browser loads the JSP page, so if you want to use it inside your javascript, you need to capture that lateRegDays variable during the page loading time as below:

<script>
var lateRegDays; <!-- global variable -->
function init() {
lateRegDays = '<%= lateRegDays %>';
}
function  displayDatePicker(...) {
  // Get the lateRegDays var
}
</script>
<body onload="init()">
<!-- your code -->
</body>

P.S.: Scriplets are legacy code (invented in early 2000 by Sun), which is not a best practice to use them, so avoid them by using the latest front-end technology stack for the presentation layer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.