Sharper
Sharper

Reputation: 357

Remove unused fields in form before submit

I my be wrong in my thinking but... I thought that if I removed the name attribute of unused form fields from a form just before submit it would remove them from the resulting email tidying things up a little. My code doesn't seem to be working very well, any help would be welcome.

<script>
  function submitRequestForm(){
   requestForm = $("#formid");
    requestForm.find('input[name], select[name]').each(function(){
    if ($(this).val(){
      $(this).removeAttr('name');
    }
    requestForm.submit();
  }
</script>

Upvotes: 1

Views: 1829

Answers (1)

mohamedrias
mohamedrias

Reputation: 18566

Instead of removing the name attribute, try disabling the field during form submit.

Before that there are few syntax errors in your code.

 function submitRequestForm(){
   requestForm = $("#formid");
    requestForm.find(":input").filter(function(){ return !this.value; }).attr("disabled", "disabled");
    requestForm.submit();

   // In case you want to re-enable the fields
   requestForm.find(":input").filter(function(){ return !this.value; }).removeAttr("disabled");

  }

Upvotes: 3

Related Questions