Identify input ID

Asked

Viewed 85 times

0

Guys, a help, I think I’m no longer able to confirm where I’m going wrong.

It has a form with 4 fields (item, description, unit and quantity) and a button to add more lines if you want, I use the code below to add new lines;

$(".adicionarCampo").click(function () {
novoCampo = $("tr.linhas:first").clone();
novoCampo.find("input").val("");
novoCampo.insertAfter("tr.linhas:last");
removeCampo();
});

This is the button to add more lines

<input type="button" name="Adicionar" id="Adicionar" class="adicionarCampo" value="+"> 

The user enters an item and the description field auto-populated with the code below.

function update(cod_atividade){
$.post("../../scripts/codigos_atividades.php", {cod_atividade:cod_atividade}, function(retorno){

dados = retorno.split("/");
 $('#descricao').val(dados[5]);
 });
}

Take the input ID with name description, let it read only and the above script puts the description information as per the item

<input name="descricao[]" type="text" required="required" class="descricao" id="descricao" size="40" maxlength="255" readonly="readonly" />

The problem is that if the user adds a new field(line) and puts an item, the description of this new item changes only in the first input, if he adds 10 fields, he keeps changing only in the first input, and not in that respective input.

I know that the ID is unique and in this case it really changes the first line, my problem is that I do not know how to make the script that puts the description identify which id is now without affecting those that have already been made.

I’ve tried making the id field with the brackets at the end but it doesn’t work.

<input name="descricao[]" type="text" required="required" class="descricao" id="descricao[]" size="40" maxlength="255" readonly="readonly" />

I would like an idea of how to do it. I appreciate this now.

EDIT:

For you to understand what happens.

I added new lines and put some item in the first line. Item G.10.05.005.095 is Camera

Then I clicked on add new lines (in yellow) and insert another item any F.30.70.005.010 is Key Machine. Instead of the item below adding the description in his line, he changes the top line.

Adicionando o item G.10.05.005.095 na primeira linha

Adicionando o item F.30.70.005.010 e ele muda também a primeir linha, ao inves de mudar na dele

  • but do you really need the ID? it has to be unique and it would be a job to keep track of it and assign Ids, I think you can use the name name and read that figure. This is a very common scenario, to add items manually, see this question, has an example and does not use ID: https://answall.com/questions/492776/somar-mais-um-id-ao-gerar-mais-um-input-via-javascript If you want more help, put the whole functional part of the code and mount an example demonstrating the problem

  • And if you create a variable containing numbers whose initial value is 0, and when you create the new input it puts the input id something + the value of that variable and increment the value of the variable in 1, would it not solve? I don’t quite understand your problem.

  • Hello Ricardo and Isaque, thanks for interacting, do not necessarily need to be the ID, can be by name or class, for example. I edited the question so you’ll understand what happens.

  • @Fidelessanches, without seeing the html is difficult to help, but is using a clone, If you clone an element that has an ID it will generate a problem because the ID must be unique, and if you have any reference to it, no matter how many lines you insert, it will reference the first element. If you’re gonna do clone(), remove the Ids, this will edit issues. id="descricao[]" this does not work as already noticed, so remove ids

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.