954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

scriptlet in javascript

hii..
I want to use a scriplet variable in javascript.
i used
<%! int i;>
<% i=0; %>
')>

it works fine if i give alert(i) in
changeValue function.
Otherwise it gives error i is undefined.
Plz help me. I don't want to bother user by increasing one user click.
Plz help what is the reason ????

i m using struts 1.2

:confused:

mamatachaudhari
Newbie Poster
20 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Try this:
changeValue('<%=i%>')

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
Try this: changeValue('<%=i%>')

Thanks for reply....
It's not working..
when i first time load my project it gives me error like i is undefined
But after giving an alert it works fine...

mamatachaudhari
Newbie Poster
20 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
Thanks for reply.... It's not working.. when i first time load my project it gives me error like i is undefined But after giving an alert it works fine...

What do you mean giving an alert. Post the code that works

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
What do you mean giving an alert. Post the code that works

This is my input text field

<input type="text" id="productQuantity" size="5"  onchange="changeQuantity(this.form,'<bean:write property="productCode" name="product"/>','<%=z %>');"/>


And this is my javaScript function is..

function changeQuantity(form,productCode,index)
			{
				//alert("z==="+productCode);
				lengthOfItems=form.productCombo.options.length;
				lengthOfProducts=form.productCombo2.options.length;
alert("index"+index);
changeItemValueNew(form,productCode,index);
}


after giving that alert messsage it works otherwise not.

}

mamatachaudhari
Newbie Poster
20 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

First of all describe better what do you mean by this:
after giving that alert messsage it works otherwise not.

Also, firstly does the method being called correctly?

Try this to check the above:

function changeQuantity(form,productCode,index)
{
alert("Method called");
}


Then see if the arguments are passed correctly:

function changeQuantity(form,productCode,index)
{
alert(productCode);
alert(index);
}


Then try your code with alert messages between the other commands (like debugging) to see what is wrong

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Create the variable in the javaScript
and store the value in it
then use it

function changeQuantity(form,productCode,index)
{
 var   z=productCode;
var y=index;
lengthOfItems=form.productCombo.options.length;
lengthOfProducts=form.productCombo2.options.length;
alert("index"+index);
changeItemValueNew(form,z,y);
}
amarjeetsingh
Newbie Poster
21 posts since Feb 2009
Reputation Points: 2
Solved Threads: 1
 

Create the variable in the javaScript and store the value in it then use it

var   z=productCode;
var y=index;



I don't believe that it will solve the problem. If this will execute correctly:

var   z=productCode;

changeItemValueNew(form,z,y)

then this will also:

changeItemValueNew(form,productCode,index);
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Create the variable in the javaScript
and store the value in it
then use it

function changeQuantity(form,productCode,index)
{
 var   z=productCode;
var y=index;
lengthOfItems=form.productCombo.options.length;
lengthOfProducts=form.productCombo2.options.length;
alert("index"+y);
changeItemValueNew(form,z,y);
}


if you do calulation in java script, then you must be attentative
about the data type of the variable

java script is not strictly check the datatype of the variable

i do n't know what have you done in

changeItemValueNew(form,z,y)


method

amarjeetsingh
Newbie Poster
21 posts since Feb 2009
Reputation Points: 2
Solved Threads: 1
 

i do n't know what have you done in

changeItemValueNew(form,z,y)

method

We still don't know if the code before the call of that method is executed

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Create the variable in the javaScript and store the value in it then use it

function changeQuantity(form,productCode,index)
{
 var   z=productCode;
var y=index;
lengthOfItems=form.productCombo.options.length;
lengthOfProducts=form.productCombo2.options.length;
alert("index"+index);
changeItemValueNew(form,z,y);
}


thanks.. Its working perfectly

mamatachaudhari
Newbie Poster
20 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You