Javascript - Remove hidden fields or set to empty before form submission

13,693

Simply change the onclick to this:

onclick="document.getElementById('ci').value = ''; document.getElementById('pg').value = ''; this.form.submit();"

You just have to use the ID of the elements then assign their value to empty string - this will still send value though. To disable any value, disable those elements:

onclick="document.getElementById('ci').disabled = true; document.getElementById('pg').disabled = true; this.form.submit();"
Share:
13,693
user558122
Author by

user558122

Updated on June 04, 2022

Comments

  • user558122
    user558122 almost 2 years

    I want to either remove 2 hidden fields or set them to null in a form before a POST action is performed using onclick event on a checkbox.

    This is what I have -

    <input type="hidden" id="ci" name="ci" value="2"/>
    <input type="hidden" id="pg" name="pg" value="prev"/>
    
    <form:checkboxes path="choices" items="${lookups}" itemValue="id" itemLabel="label" element="li" **onclick="this.form.submit();"**/>
    

    How do I modify my onclick event JS to support this ?

    Thanks!