title: How to assign a javascript variable to a hidden input?
body: <p>I wanted to store a javascript variable to a hidden input field because I wanted to get it from a servlet. This is my current code:</p>
  
  <p>HTML:
  </p>
  
  <pre><code>&lt;form action="myServlet"&gt;
  
  
  &lt;input type="hidden" id="id" value="" name="total"&gt;
       &lt;input type="submit" value="Go!"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre class="lang-js prettyprint-override"><code>var a = 2;
  document.getElementById('id').value=a;
  </code></pre>
  
  <p>myServlet (java servlet):</p>
  
  <pre><code>int count = Integer.parseInt(request.getParameter("total"));
          ServletContext context= getServletContext();
          request.setAttribute("count", count);
              RequestDispatcher rd= context.getRequestDispatcher("/newjsp1.jsp");
              rd.forward(request, response);
  </code></pre>
  
  <p>whenever I click the submit button, it gives the error <code>NumberFormatException</code> because the javascript variable is not assigned to the hidden input field that i'm trying to get using a servlet. I hope you guys can help me with my dilemma. Thanks! </p>