• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

how to pass variable to a javascript function

Hi,

The following javascript program does not work. How to pass a variable or expression to a javascript function? I am going to use similar code in
a jsp page.

<head>
<script>
function calculate(i){
....
</script>
</head>

<%
int iCount = 1;
%>
      

<html:text name="" property="" onchange='calculate(<%= iCount %>)' />


Thanks.
0
victorli
Asked:
victorli
1 Solution
 
BatalfCommented:
Your example should successfully pass the value "1" to the function calculate. If it doesn't then there's something else that's wrong and we need to see more code to figure it out(The source output to the browser).

If you're trying to pass the value of the text field to the function then it should be

<html:text name="" property="" onchange='calculate(this.value)' />

Batalf

0
 
Ramesh SrinivasTechnical ConsultantCommented:
<html:text name="" property="" onchange='calculate(1)' />

Try that, if it doesnt work then there is somthing wrong with your function.
0
 
arun99907Commented:
First check if the java script function 'calculate(<%=iCount%>)' is being called or not?

Try to keep an alert like this:

<head>
<script>
var i;
function calculate(i){
alert('inside');
alert(i);
....
</script>
</head>

<%
int iCount = 1;
%>
<html:text name="" property="" onchange='calculate(<%= iCount %>)' />

and verify if it is correctly being called.

Can u clearly tell what is the error that you are getting ?
Can u also try giving the name="abc" instead of "" ? what is the property you are referring to ?

see to it that atleast the function gets called first. If the function is not getting called then it is error in your
<html:text name ...../>
so check this out clearly.
0
 
victorliAuthor Commented:
Hi all,

Sorry for the delay to respond to you.

I tried and found the Javascript do take expressions in JSP and the above example works.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now