How to avoid sending input fields which are hidden by display:none to a server?

Viewed 80420

Imagine you have a form where you switch visibility of several fields. And if the field is not displayed you don't want its value to be in request.

How do you handle this situation?

7 Answers

What I did was just remove the elements entirely when the form is submitted:

var myForm = document.getElementById('my-form')

myForm.addEventListener('submit', function (e) {
  e.preventDefault()

  var form = e.currentTarget
  var hidden = form.getElementsByClassName('hidden')

  while (hidden.length > 0) {
    for (var i = 0; i < hidden.length; i++) {
      hidden[i].remove()
    }
  }

  form.submit()
})
Related