3
I’m new here and I’m curious about programming, so forgive me if I talk nonsense, but I need help.
I have a form with the option to insert inputs dynamically.
Whenever an "item" is added, two inputs, the "foo" and the "bar".
All the "foo" relate to the datalist "list".
All the "bar" are inactive and its value must be changed whenever the value of "foo" is amended.
HTML INPUTS:
<div id="lista">
<div>
<input name="foo[]" list="dList" type="text">
<inpu name="bar[]" disabled="" type="text">
</div>
<div>
<input name="foo[]" list="dList" type="text">
<inpu name="bar[]" disabled="" type="text">
</div>
</div>
<datalist id="dList">
<option label="Item 1" datavalue="1" value="Item 1">
<option label="Item 2" datavalue="2" value="Item 2">
</datalist>
The only way I found to get the inputs (dynamics) was:
input = $('#listaServicos').find(':input'); // array de inputs
// aqui tereis tanto os _"foo"_ quanto os _"bar"_
And then make a for
working with them:
valores = [[1,3000],[2,5000],[3,4000],[4,10000],[5,10000]];
for (i = 0; i < input.length; i++) {
name = input[i].name;
console.log("name: "+ name);
if(name.indexOf('foo') > -1){
// aqui preciso pegar o atributo _"datavalue"_ do datalist
// correspondente, comparar com a key array valores
//e preencher o _"bar"_ com o valor
}
}
The biggest problem is in relating the "foo" with the datalist, because the "foo" and the "bar" will always be in the same subarray.
Thanks in advance for any help that comes
Great response!!! Grateful for the help, I didn’t expect such a precise and fast response!
– Paulo Junior
Additionally, every time an input is added, I have to Re-enter the "$('input'). on('input', Function() {.... }" to work on new inputs.
– Paulo Junior