Nathan, I believe you may be encountering some problem with the scope of the variables, between trying to make the following changes.
try to use some more structured way to group your elemethos, whether using a namespace
, one class
, one data-custom
Since your code already uses something similar to a namespace, then I’ll post an example using namespace:
var campos = document.querySelectorAll("[id|='campo']");
[].forEach.call(campos, function (campo, indice) {
console.log(campo, indice);
});
<input id="campo-1" type="text" />
<input id="campo-2" type="text" />
<input id="campo-3" type="text" />
<input id="campo-4" type="text" />
<input id="campo-5" type="text" />
<input id="campo-6" type="text" />
<input id="campos" type="text" />
the selector [campo|='namespace']
, will take all elements whose value of the selected field (in the case id
) possess the namespace
informed, that is to say start with a given string
follow of a hifen
, for this in the above example input#campos
is not captured by the selector.
how are you passing your cont
to the funcaoB
, I believe you’re redoing the consultation (getElementById
), then I advise you to pass the input#campo-n
.
var campos = document.querySelectorAll("[id|='campo']");
var funcaoB = function (campo, indice) {
window.setTimeout(function () {
console.log(campo.id, campo.type);
}, (indice + 1) * 1000)
};
[].forEach.call(campos, function (campo, indice) {
funcaoB(campo, indice);
});
<input id="campo-1" type="text" />
<input id="campo-2" type="number" />
<input id="campo-3" type="date" />
<input id="campo-4" type="time" />
<input id="campo-5" type="email" />
<input id="campo-6" type="tel" />
<input id="campos" type="text" />
note that in the example above, even making use of the window.setTimeout
to simulate a request AJAX
, I had no problem getting the id
and the type
of each input
.
in any case it becomes difficult to help you without seeing the whole script, after all your problem is possibly ma funcaoB
One hour to variable
cont
is numerical, another is string, This obviously won’t work. What do you want to do? Give relevant information to solve the problem, if possible put a functional example. How do you make sure that the function is not executed? How are the elements in HTML?– Maniero
Function B is a verification function, the user enters information in the field and it returns if this information is registered in the database. If true, it blocks the button next to the field to add the information to the BD, if false, it leaves the button free to include the data in the BD. In case the functionA makes the inclusion of the data in the BD and runs the function again so that when the pop-up window is closed the input button is blocked.
– Nathan
Wouldn’t it be better to do
while (cont > 0)
,else cont = 0;
? than converting from numeric to string.– Guilherme Lautert
From what I understand, your function is ok, in case you want to check fields
1_campo
,2_campo
..., Specify the problem better.– Guilherme Lautert
In the case while is executed until it has been verified how many fields there are in the document. The fields are named as follows: 1_field, 2_field... They can be added or removed according to the amount of information to be saved at once, so cont gets 'p', that is until there are fields it will call function B, when it calls Document.getElementById(cont+'_field') and equals null cont takes p and ends.
– Nathan
You’re talking... Function B is a verification function, the user enters information in the field and it returns if this information is registered in the database, you go into a database to check this? why don’t you just do everything on the server side?
– Marco Souza
Yes, she takes what is typed in the field and searches the database if that information is already registered
– Nathan
It can be a lot of stuff, and it seems that the problem is in function B. You can post her code?
– bfavaretto
The problem is not in function B, because I run it to check the entered data and it works normally, the problem is when it is called by function A. If executed only once, it works, but from the second interaction on.
– Nathan