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.form1.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.disabled = true;
document.form1.notapp1.disabled = true;
document.form1.notapp2.disabled = true;
document.form1.notapp3.disabled = true;
document.form1.notapp4.disabled = true;
document.form1.notapp5.disabled = true;
document.form1.notapp6.disabled = true;
document.form1.notapp7.disabled = true;
document.form1.notapp8.disabled = true;
document.form1.notapp9.disabled = true;
document.form1.notapp10.disabled = true;
....
document.form1.notapp90.disabled = true;
document.form1.notapp91.disabled = true;
document.form1.notapp92.disabled = true;
document.form1.notapp93.disabled = true;
document.form1.notapp94.disabled = true;
document.form1.notapp95.disabled = true;
document.form1.notapp96.disabled = true;
document.form1.notapp97.disabled = true;
document.form1.notapp98.disabled = true;
document.form1.notapp99.disabled = true;
 
}

Can someone explain to me what im doing wrong and why the loop is not stopping correctly , thank you.
WarriorHypnotistAsked:
Who is Participating?
 
sompol_kiatkamolchaiCommented:
sorry. for get count++.

function Disable()
{
     for (count=0;count<=<%=session.getAttribute("num")%>;count++)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}
0
 
apexpertCommented:
Please try it now:
<script language=javascript>
var d ='<%=session.getAttribute("num")%>';

function Disable()
{
     
      <%
      int xxx = 0;
      while(xxx < d)
      {
              out.println("document.form1.notapp" + xxx + ".disabled = true;");
              xxx++;
      }
      %>
}
0
 
Sathish David Kumar NArchitectCommented:
u have mention as 'd' as string not numeric !

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
WarriorHypnotistAuthor Commented:
i get this error when i remove the ''
org.apache.jasper.JasperException: 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.form1.notapp" + xxx + ".disabled = true;");
33:               xxx++;


Stacktrace:
      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
0
 
Gurvinder Pal SinghCommented:
why have you given
var d ='<%=session.getAttribute("num")%>';

try with this
var d =<%=session.getAttribute("num")%>;
0
 
Gurvinder Pal SinghCommented:
or do

function Disable()
{
     var intVal = parseInt(d);
      <%
      int xxx = 0;
     
      while(xxx < intVal )
      {
              out.println("document.form1.notapp" + xxx + ".disabled = true;");
              xxx++;
      }
      %>
}
0
 
WarriorHypnotistAuthor Commented:
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.form1.notapp" + xxx + ".disabled = true;");
29:               xxx++;


Stacktrace:
      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
0
 
WarriorHypnotistAuthor Commented:
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("num")%>;
 

 
function Disable()
{
      var intVal = parseInt(d);
      <%
      int xxx = 0;
      
      while(xxx < intVal)
      {
              out.println("document.form1.notapp" + xxx + ".disabled = true;");
              xxx++;
      }
      %>
}
<%
0
 
Gurvinder Pal SinghCommented:
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.getAttribute("num")%>;var=var+1)
     {
       document.write("document.form1.notapp" + xxx + ".disabled = true;");
     }
}
0
 
Gurvinder Pal SinghCommented:
sorry, use this one

function Disable()
{
     for (var=0;var<=<%=session.getAttribute("num")%>;var=var+1)
     {
       document.form1.notapp<%=session.getAttribute("num")%>.disabled = true;
     }
}
0
 
WarriorHypnotistAuthor Commented:
its not looping ??!

function Disable()
{
     for (var=0;var<=4;var=var+1)
     {
       document.form1.notapp4.disabled = true;
     }
}
0
 
Gurvinder Pal SinghCommented:
ok, try this

function Disable()
{
     for (var=0;var<=4;var=var+1)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}

0
 
WarriorHypnotistAuthor Commented:
not working as well , also the 4 value may change according to the session.getAttribute("num")

0
 
sompol_kiatkamolchaiCommented:
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)
0
 
Gurvinder Pal SinghCommented:
"not working as well , also the 4 value may change according to the session.getAttribute("num")"

I know sir.

you should have tried that yourself

function Disable()
{
     for (var=0;var<=<%=session.getAttribute("num")%>;var=var+1)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}
0
 
WarriorHypnotistAuthor Commented:
ok since 'd' is returning a value of 4 how can i convert it to make it usable number in the loop ?
0
 
sompol_kiatkamolchaiCommented:
Try this one.

function Disable()
{
     for (count=0;count<=<%=session.getAttribute("num")%>;count)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}
0
 
sompol_kiatkamolchaiCommented:
the line

var elementName = notapp + "" + var;

Should be

var elemetName = notapp + "" + count;

Sorry for typo error
0
 
WarriorHypnotistAuthor Commented:
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(elementName).disabled =  true;
     }
}
but it does not printing the 4 lines like it should
document.form1.notapp0.disabled = true;
document.form1.notapp1.disabled = true;
document.form1.notapp2.disabled = true;
document.form1.notapp3.disabled = true;

0
 
Gurvinder Pal SinghCommented:
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.

0
 
WarriorHypnotistAuthor Commented:
ok let me check and i will get back to u
0
 
sompol_kiatkamolchaiCommented:
it should be

var elementName = "notapp" + count;

Hope this help,
Sompol
0
 
WarriorHypnotistAuthor Commented:
its working now , i wanted 2 print the lines to see the code but i think your solution is the better one.
0
 
WarriorHypnotistAuthor Commented:
the final funcion is
function Disable()
{
     alert(<%=session.getAttribute("num")%>);
     for (count=0;count<=<%=session.getAttribute("num")%>;count++)
     {
       var elementName = "notapp" + count;
       document.getElementById(elementName).disabled = true;
       
     }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.