java - How to assign a javascript variable to a hidden input? -


i wanted store javascript variable hidden input field because wanted servlet. current code:

html:

<form action="myservlet">   <input type="hidden" id="id" value="" name="total">      <input type="submit" value="go!"/> </form> 

javascript:

var = 2; document.getelementbyid('id').value=a; 

myservlet (java servlet):

int count = integer.parseint(request.getparameter("total"));         servletcontext context= getservletcontext();         request.setattribute("count", count);             requestdispatcher rd= context.getrequestdispatcher("/newjsp1.jsp");             rd.forward(request, response); 

whenever click submit button, gives error numberformatexception because javascript variable not assigned hidden input field i'm trying using servlet. hope guys can me dilemma. thanks!

you have add javascript after input hidden element rendered, ie order of code arrangement important since have added javascript execute when rendered in browser . make sure no other element has same id value "id".

i prefer use function execute on form submit follows.

    <form action="myservlet" onsubmit="setval();"> <input type="hidden" id="id" value="" name="total">      <input type="submit" value="go!"/>  </form>  <script>          function setval(){    var = 2;     document.getelementbyid('id').value=a; } </script>  

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -