-1

I have a jsp page wherein I've used javascript function and I'm calling this using onClick present inside a form:

<script type="text/javascript">
function fa(){

    <%
String as = null;
int aa =10;
for(int q =0; q<=aa ; q++){
    if(q==5){
    as="asds";
    }
}
%>
    <%= as%>
}
</script>

I'm a little confused about using javascript in a jsp page. How do I use the for loop? Usually, for loop can be called directly inside a js but since this is a jsp page, how will I use it? Do I have to call forEach instead? Right now, this code does not work.

Mercenary
  • 2,106
  • 6
  • 34
  • 43
  • why do you need to put the for loop in the jsp expression? – BMH Aug 14 '13 at 04:57
  • Actually, this is a sample data. I'm actually using a bean and fetching its methods and values. So, I put the whole thing in the scriplet! After all, it is java code!! – Mercenary Aug 14 '13 at 05:01

1 Answers1

1

You can use the for loop of JavaScript itself.

<script type="text/javascript">
function fa(){
var as = null;
var aa =10;
for(var q =0; q<=(Number)aa ; q++){
    if((Number)q==(Number)5){
    as="asds";
    }
}
alert(as);
}
</script>
Dileep
  • 5,362
  • 3
  • 22
  • 38
  • Thanks for this! If I'm fetching some value from the bean and I'm assigning it to say `var i1` and if that value is a Boolean/String, can I call `i1` in other places like this : `(Boolean)i1` and `(String)i1` – Mercenary Aug 14 '13 at 05:08