0

I have a form, and I'm grabbing all input elements inside of it and place them into an their keys and values into an object (That works)... However I wan't to filter out the un needed button... How can I remove it/ ignore it?

if I console.log form.elements it returns:

[input#first_name.input-text, input#last_name.input-text, input#username.input-text, input#email.input-text, 
button#submit.btn ...]

  const formatData = {};
  const form = document.getElementById('signup');
  console.log(form.elements);

  for (let i = 1; i < myForm.elements.length; i++) {
      // check if button and remove / ignore
      if (form[i] === 'button') { // this doesn't work
          delete form[i];
      }
      const key = form.elements[i].name;
      const value = form.elements[i].value;
      formatData[key] = value;

How can I make sure button isn't included? :\ (this is written in jsx syntax)

14
  • try if (form[i].type === 'button') Commented Jun 9, 2016 at 15:47
  • @DimaGimburg still shows :\ Commented Jun 9, 2016 at 15:49
  • I feel like form[i].tagName === 'BUTTON'should work. element.type also works on Chrome, but I can't find this property in the WebAPI docs. Note, that it's capitalised. See MDN. Commented Jun 9, 2016 at 15:51
  • @DimaGimburg if i log .type it returns text, hidden and submit.. I tried submit but it still shows. I think its how i should be deleting it. Commented Jun 9, 2016 at 15:53
  • @Kazimieras .type returns text, hidden and submit Commented Jun 9, 2016 at 15:53

3 Answers 3

1

You can filter form.elements by tagName.

const formatData = {};
const form = document.getElementById('signup');

[].slice.apply( form.elements ) // convert HTMLFormControlsCollection to Array
  .filter(( el ) => el.tagName !== 'BUTTON') // filter out all <button> elements
  .forEach(function ( el ) {
      formatData[ el.name ] = el.value; // add key / value to formatData object
  });

OR, if you prefer the for loop:

const formatData = {};
const form = document.getElementById('signup');

for (let i = 0, n = form.elements.length; i < n; i++) {
  if (form.elements[i].tagName === 'BUTTON') continue;

  formatData[ form.elements[i].name ] = form.elements[i].value;
}
Sign up to request clarification or add additional context in comments.

4 Comments

That didn't work either. I just might have to deal with this on the backend
Actually how can i also filter out hidden inputs with your first example?
@Modelesq - Array.reduce is your friend - provided functionality is more readable, refactors better and can be shared (better code reuse) - see my answer below.
@Modelesq - When you say filter out hidden inputs, do you mean <input type="hidden"> elements?
0
 for (let i = 1; i < myForm.elements.length; i++) {
      if (form[i].type !== 'button') { // or as suggested form[i].tagName !== 'BUTTON'
          const key = form.elements[i].name;
          const value = form.elements[i].value;
          formatData[key] = value;
      }

and don't you want to loop through all the elements and start the i with 0?

2 Comments

ah yeah thats a typo on my end. thank you for catching that
hmm if i run console.log(form.elements); it's still in there :\
0
var
    elementCollection = document.forms[document.forms.length -1].elements,
    elementRegistry   = Array.from(

        elementCollection

    ).reduce(function (collector, elm) {
        if (
               (elm.tagName.toLowerCase() !== 'button')
            && !collector.regXIgnoreTypes.test(elm.type)
        ) {
          //var key = (elm.id || elm.name);
            var key = elm.name;
            if (key !== '') {

                collector.elementRegistry[key] = elm;
            }
        }
        return collector;
    }, {

      regXIgnoreTypes: (/^(?:button|submit|image|hidden)$/),
      elementRegistry: {}

    }).elementRegistry;

console.log('+++ elementRegistry +++', elementRegistry);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.