Home > other >  Javascript: how get all the values and id from an inputs array?
Javascript: how get all the values and id from an inputs array?

Time:10-20

So I'm trying to get all the values and id of every input from an array, but I have an issue, the user can duplicate the input and each input have a randomly generated ID so I can't select the data with the ID and every input use name="cate[]" so every input have the same name, so I can use something like this:

function regTour() {
    var input = document.getElementsByName('cat[]');
    var k = "The respective values are :";

    for (var i = 0; i < input.length; i  ) {
        var a = input[i];
        k = k   "array["   i   "].value= "
                             a.value   " ";
    }
    console.log('Console: '   k);
}

but how I can get the ID from every input from the array paired with the input value?

Edit: -------------------

the input looks like this:

    <input type="text" id="1666239497429" name="cat[]" >
<input type="text" id="12983172462" name="cat[]" >
<input type="text" id="812361647812" name="cat[]" >

this input it's created dynamically by the user so the id changes, this id later it's stored with other sub fields that should be related with that id, that's why it's some kind important for me be able to store all the id's

CodePudding user response:

function regTour() {
    var input = document.getElementsByName('cat[]');
    var k = "The respective values are :";

    for (var i = 0; i < input.length; i  ) {
        var a = input[i];
        const id = a.id;
        k = k   "array["   i   "].value= "
                             a.value   " array["   i   "].id= "   a.id;
    }
    console.log('Console: '   k);
}

CodePudding user response:

can you share more data from the front? how is your form?

This ist OK var input = document.getElementsByName('cat[]'); but I'm not sure what the problem is

  • Related