Search code examples
javajavascriptjspjsp-tags

how a jsp function execute when javascript's function call?


<script language="javascript"> 
var counter=0; 
var i=1;
function saveclick() 
{ 
<%j++;%>

    if(counter==3)
    { 
alert(counter);

}


else {

document.getElementById("error").innerHTML ="<%=qarr[j]%>"

  }
}

</script>

here i am using a qarr an array of String and its return String of corresponding index of array. But when i click on button this javascripts' saveclick function execute but innerhtml shows only one String all times. I want that when i click on button then js function execute and every time new String appear in innerHtml. please do needful in this matter.


Solution

  • Jsp plays on server side and javascript plays on client side.

    Java needs compiled code and Javascript is just a scripting language interpreted by the browser.

    You need to either make a request to server(HTML forms/AJAX) for new content or Maintain Json object in client side itself while page loading and use it later.

    But You cannot mix them.