title: make a field mandatory using javascript
body: <p>I am trying to make a select field mandatory on a web page. I know how to do it with help of JS and form attribute 'onsubmit' and returning the function. But the problem is that form code is already written and I dont know how to add attribute now. Let me know if I can append attribute dynamically from JS. 
  The other way I tried is to call the JS after page loaded. But this isnt making the field mandatory and form can be submitted.
  Following is my code..</p>
  
  <pre><code>       &lt;!DOCTYPE html&gt;
         &lt;html&gt;
         &lt;head&gt;
         &lt;script&gt;
         function f1()
         {
             var countryValue = document.getElementById('count ID').value;
  
      if (countryValue == "") 
      {
              alert("field value missing");
          return false;
      }
      var stateValue = document.getElementById('state ID').value;
      if (stateValue == "") 
      {
              alert("state field value missing");
          return false;
      }
  }
  
        &lt;/script&gt;
       &lt;/head&gt;
       &lt;body&gt;
      &lt;form method = "post" action = "33.html"&gt;
      Country: &lt;input type="text" id="count ID"&gt;
      state: &lt;select id="state ID"&gt; 
               &lt;option&gt;&lt;/option&gt;
                       &lt;option value="ap"&gt;ap&lt;/option&gt; 
                       &lt;option value="bp"&gt;bp&lt;/option&gt; 
                      &lt;/select&gt;
      &lt;br&gt;
      &lt;input type = "submit"&gt;
      &lt;/form&gt;
      &lt;script&gt;window.onload=f1&lt;/script&gt;
  
              &lt;/body&gt;
              &lt;/html&gt;
  </code></pre>
  
  <p>Please help. </p>