WarriorHypnotist
asked on
Javascript while loop with jsp passed value error
Im trying to create a javascirpt function with jsp code , i have the following :-
<script language=javascript>
var d ='<%=session.getAttribute( "num")%>';
function Disable()
{
<%
int xxx = 0;
while(xxx < 'd')
{
out.println("document.form 1.notapp" + xxx + ".disabled = true;");
xxx++;
}
%>
}
The value of d is 4 , but when i run the code it gives up to 100 raws as below
<script language=javascript>
var d ='4';
function Disable()
{
document.form1.notapp0.dis abled = true;
document.form1.notapp1.dis abled = true;
document.form1.notapp2.dis abled = true;
document.form1.notapp3.dis abled = true;
document.form1.notapp4.dis abled = true;
document.form1.notapp5.dis abled = true;
document.form1.notapp6.dis abled = true;
document.form1.notapp7.dis abled = true;
document.form1.notapp8.dis abled = true;
document.form1.notapp9.dis abled = true;
document.form1.notapp10.di sabled = true;
....
document.form1.notapp90.di sabled = true;
document.form1.notapp91.di sabled = true;
document.form1.notapp92.di sabled = true;
document.form1.notapp93.di sabled = true;
document.form1.notapp94.di sabled = true;
document.form1.notapp95.di sabled = true;
document.form1.notapp96.di sabled = true;
document.form1.notapp97.di sabled = true;
document.form1.notapp98.di sabled = true;
document.form1.notapp99.di sabled = true;
}
Can someone explain to me what im doing wrong and why the loop is not stopping correctly , thank you.
<script language=javascript>
var d ='<%=session.getAttribute(
function Disable()
{
<%
int xxx = 0;
while(xxx < 'd')
{
out.println("document.form
xxx++;
}
%>
}
The value of d is 4 , but when i run the code it gives up to 100 raws as below
<script language=javascript>
var d ='4';
function Disable()
{
document.form1.notapp0.dis
document.form1.notapp1.dis
document.form1.notapp2.dis
document.form1.notapp3.dis
document.form1.notapp4.dis
document.form1.notapp5.dis
document.form1.notapp6.dis
document.form1.notapp7.dis
document.form1.notapp8.dis
document.form1.notapp9.dis
document.form1.notapp10.di
....
document.form1.notapp90.di
document.form1.notapp91.di
document.form1.notapp92.di
document.form1.notapp93.di
document.form1.notapp94.di
document.form1.notapp95.di
document.form1.notapp96.di
document.form1.notapp97.di
document.form1.notapp98.di
document.form1.notapp99.di
}
Can someone explain to me what im doing wrong and why the loop is not stopping correctly , thank you.
u have mention as 'd' as string not numeric !
ASKER
i get this error when i remove the ''
org.apache.jasper.JasperEx ception: Unable to compile class for JSP:
An error occurred at line: 30 in the jsp file: /DepSheet.jsp
d cannot be resolved
27: <%
28: int xxx = 0;
29:
30: while(xxx < d)
31: {
32: out.println("document.form 1.notapp" + xxx + ".disabled = true;");
33: xxx++;
Stacktrace:
org.apache.jasper.compiler .DefaultEr rorHandler .javacErro r(DefaultE rrorHandle r.java:85)
org.apache.jasper.compiler .ErrorDisp atcher.jav acError(Er rorDispatc her.java:3 30)
org.apache.jasper.compiler .JDTCompil er.generat eClass(JDT Compiler.j ava:415)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:308)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:286)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:273)
org.apache.jasper.JspCompi lationCont ext.compil e(JspCompi lationCont ext.java:5 66)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 08)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:320)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:266)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
org.apache.jasper.JasperEx
An error occurred at line: 30 in the jsp file: /DepSheet.jsp
d cannot be resolved
27: <%
28: int xxx = 0;
29:
30: while(xxx < d)
31: {
32: out.println("document.form
33: xxx++;
Stacktrace:
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.JspCompi
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
why have you given
var d ='<%=session.getAttribute( "num")%>';
try with this
var d =<%=session.getAttribute(" num")%>;
var d ='<%=session.getAttribute(
try with this
var d =<%=session.getAttribute("
or do
function Disable()
{
var intVal = parseInt(d);
<%
int xxx = 0;
while(xxx < intVal )
{
out.println("document.form 1.notapp" + xxx + ".disabled = true;");
xxx++;
}
%>
}
function Disable()
{
var intVal = parseInt(d);
<%
int xxx = 0;
while(xxx < intVal )
{
out.println("document.form
xxx++;
}
%>
}
ASKER
i did what you said gurvinder372 but it is giving the same error again
An error occurred at line: 26 in the jsp file: /DepSheet.jsp
intVal cannot be resolved
23: <%
24: int xxx = 0;
25:
26: while(xxx < intVal)
27: {
28: out.println("document.form 1.notapp" + xxx + ".disabled = true;");
29: xxx++;
Stacktrace:
org.apache.jasper.compiler .DefaultEr rorHandler .javacErro r(DefaultE rrorHandle r.java:85)
org.apache.jasper.compiler .ErrorDisp atcher.jav acError(Er rorDispatc her.java:3 30)
org.apache.jasper.compiler .JDTCompil er.generat eClass(JDT Compiler.j ava:415)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:308)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:286)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:273)
org.apache.jasper.JspCompi lationCont ext.compil e(JspCompi lationCont ext.java:5 66)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 08)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:320)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:266)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
An error occurred at line: 26 in the jsp file: /DepSheet.jsp
intVal cannot be resolved
23: <%
24: int xxx = 0;
25:
26: while(xxx < intVal)
27: {
28: out.println("document.form
29: xxx++;
Stacktrace:
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.JspCompi
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
ASKER
here is is code from the begening of the page
<%@ page import = "java.util.*" %>
<%@ page import = "TimeSheet.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.text.NumberFormat" %>
<jsp:useBean id="e" scope = "application" class = "TimeSheet.Employees" />
<html>
<head>
<title>Time Sheet</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language=javascript>
var jsVar ='<%=session.getAttribute( "category" )%>';
var d = <%=session.getAttribute("n um")%>;
function Disable()
{
var intVal = parseInt(d);
<%
int xxx = 0;
while(xxx < intVal)
{
out.println("document.form 1.notapp" + xxx + ".disabled = true;");
xxx++;
}
%>
}
<%
<%@ page import = "java.util.*" %>
<%@ page import = "TimeSheet.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.text.NumberFormat" %>
<jsp:useBean id="e" scope = "application" class = "TimeSheet.Employees" />
<html>
<head>
<title>Time Sheet</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language=javascript>
var jsVar ='<%=session.getAttribute(
var d = <%=session.getAttribute("n
function Disable()
{
var intVal = parseInt(d);
<%
int xxx = 0;
while(xxx < intVal)
{
out.println("document.form
xxx++;
}
%>
}
<%
Now i got it.
you are parsing javascript variable in JSP scriplet :). which is not possible
function Disable()
{
var intVal = parseInt(d);
for (var=0;var<=<%=session.get Attribute( "num")%>;v ar=var+1)
{
document.write("document.f orm1.notap p" + xxx + ".disabled = true;");
}
}
you are parsing javascript variable in JSP scriplet :). which is not possible
function Disable()
{
var intVal = parseInt(d);
for (var=0;var<=<%=session.get
{
document.write("document.f
}
}
sorry, use this one
function Disable()
{
for (var=0;var<=<%=session.get Attribute( "num")%>;v ar=var+1)
{
document.form1.notapp<%=se ssion.getA ttribute(" num")%>.di sabled = true;
}
}
function Disable()
{
for (var=0;var<=<%=session.get
{
document.form1.notapp<%=se
}
}
ASKER
its not looping ??!
function Disable()
{
for (var=0;var<=4;var=var+1)
{
document.form1.notapp4.dis abled = true;
}
}
function Disable()
{
for (var=0;var<=4;var=var+1)
{
document.form1.notapp4.dis
}
}
ok, try this
function Disable()
{
for (var=0;var<=4;var=var+1)
{
var elementName = notapp + "" + var;
document.getElementById(el ementName) .disabled = true;
}
}
function Disable()
{
for (var=0;var<=4;var=var+1)
{
var elementName = notapp + "" + var;
document.getElementById(el
}
}
ASKER
not working as well , also the 4 value may change according to the session.getAttribute("num" )
First of all, I would like to tell you about the cause of printing 100 times, it is because of this javascript.
while(xxx < 'd')
the 'd' is represent ascii code 100 in numeric so it is equal to
while(xxx < 100)
while(xxx < 'd')
the 'd' is represent ascii code 100 in numeric so it is equal to
while(xxx < 100)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok since 'd' is returning a value of 4 how can i convert it to make it usable number in the loop ?
Try this one.
function Disable()
{
for (count=0;count<=<%=session .getAttrib ute("num") %>;count)
{
var elementName = notapp + "" + var;
document.getElementById(el ementName) .disabled = true;
}
}
function Disable()
{
for (count=0;count<=<%=session
{
var elementName = notapp + "" + var;
document.getElementById(el
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the line
var elementName = notapp + "" + var;
Should be
var elemetName = notapp + "" + count;
Sorry for typo error
var elementName = notapp + "" + var;
Should be
var elemetName = notapp + "" + count;
Sorry for typo error
ASKER
this is what i get
<script language=javascript>
var jsVar ='R';
var d = 4;
function Disable()
{
for (count=0;count<=4;count++)
{
var elementName = notapp + "" + count;
document.getElementById(el ementName) .disabled = true;
}
}
but it does not printing the 4 lines like it should
document.form1.notapp0.dis abled = true;
document.form1.notapp1.dis abled = true;
document.form1.notapp2.dis abled = true;
document.form1.notapp3.dis abled = true;
<script language=javascript>
var jsVar ='R';
var d = 4;
function Disable()
{
for (count=0;count<=4;count++)
{
var elementName = notapp + "" + count;
document.getElementById(el
}
}
but it does not printing the 4 lines like it should
document.form1.notapp0.dis
document.form1.notapp1.dis
document.form1.notapp2.dis
document.form1.notapp3.dis
why do you need to print the lines?
if the requirement is to disable the elements prefixed with 'notapp', then it will be achieved with this also.
if the requirement is to disable the elements prefixed with 'notapp', then it will be achieved with this also.
ASKER
ok let me check and i will get back to u
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
its working now , i wanted 2 print the lines to see the code but i think your solution is the better one.
ASKER
the final funcion is
function Disable()
{
alert(<%=session.getAttrib ute("num") %>);
for (count=0;count<=<%=session .getAttrib ute("num") %>;count++ )
{
var elementName = "notapp" + count;
document.getElementById(el ementName) .disabled = true;
}
}
function Disable()
{
alert(<%=session.getAttrib
for (count=0;count<=<%=session
{
var elementName = "notapp" + count;
document.getElementById(el
}
}
<script language=javascript>
var d ='<%=session.getAttribute(
function Disable()
{
<%
int xxx = 0;
while(xxx < d)
{
out.println("document.form
xxx++;
}
%>
}