hii..
I want to use a scriplet variable in javascript.
i used
<%! int i;>
<% i=0; %>
<input type="text" id="itemValue" onchange=changeValue('<%i%>')>

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:

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

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...

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

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.

}

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

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);
}

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);

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.