1

On submitting a form I am getting a form object and making object. Now situation is I have input type array, for example I have input type "Task links" on click plus sign each time I am adding new input type text up to five. It could be three, two, five or in case I increase up to twenty in future, how I will handle it in javascript to make object, see the code below

See screenshot for better understanding

<input type="text" name="arr[]">
<input type="text" name="arr[]">

function submit_task_form (form_obj) {

    var get_form = document.getElementById(form_obj);

    var form_data = {};
    for(var i=0; i<get_form.length; i++) {
        form_data.task_link = get_form[i];
    }
7
  • There's no such thing as a "JSON Object" Commented Nov 10, 2017 at 11:42
  • oh ya thats right just object I mean Commented Nov 10, 2017 at 11:42
  • thank you for correcting me Commented Nov 10, 2017 at 11:43
  • I just fix it above Commented Nov 10, 2017 at 11:44
  • Are you sure you want an object rather than an array. If it's an object you want, what are the keys because all you're doing at the moment is replacing task_link in the object each time with the link from the next input in the iteration. Commented Nov 10, 2017 at 11:47

1 Answer 1

2

It's not really clear whether you need an object or an array, so here's a couple of examples that show both. These examples are in ES6. I can rewrite them in ES5 if you're not familiar with the syntax.

Both examples assume that there is a class called link on the input elements (to differentiate them from any other inputs you might have).

<input class="link" value="sdfs" />

1) Array DEMO

Pick up the inputs and iterate over them with map:

function getLinks() {
  const links = document.querySelectorAll('.link');
  return [...links].map(link => link.value);
}

OUTPUT

[ "sdfs", "34", "min", "987" ]

ES5 syntax DEMO

function getLinks() {
  var links = document.querySelectorAll('.link');
  var arr = [];
  for (var i = 0; i < links.length; i++) {
    arr.push(links[i].value);
  }
  return arr;
}

2) Object DEMO

This utilises a data-id attribute as the key for the link values.

<input class="link" data-id="1" value="sdfs" />

This time we create an object with reduce instead.

function getLinks() {
  const links = document.querySelectorAll('.link');
  return [...links].reduce((obj, link) => {
    const id = link.dataset.id;
    obj[id] = link.value;
    return obj;
  }, {});
}

OUTPUT

{
  1: "sdfs"
  2: "34"
  3: "min"
  4: "987"
}

ES5 Syntax DEMO

function getLinks() {
  const links = document.querySelectorAll('.link');
  var obj = {};
  for (var i = 0; i < links.length; i++) {
    let id = links[i].dataset.id;
    obj[id] = links[i].value;
  }
  return obj;
}
Sign up to request clarification or add additional context in comments.

8 Comments

I didnt understand the syntax
I need an object to pass it to php via ajax at the end
data: {action: "submit_task_form_with_ajax", form_data: form_data},
or I think I can assign an array of input types to the key of object
I don't know, any suggestion :)
|

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.