I'm using ViaServ's WebService to do the street autocomplete in my registration form, however it's a form where 20 lines, each line contains:
ZIP + STATE OF THE CONDO
when filling in the zip it has to autocomplete the street only referring to that cep of the same line, I tried using $("cep").next("rua").val(dados.lougradouro)
and I could not.
// HTML
<table>
<tbody>
<tr>
<td><input type="text" name="cep_streetcond[]"></td>
<td><input type="text" name="street_streetcond[]"></td>
</tr>
<tr>
<td><input type="text" name="cep_streetcond[]"></td>
<td><input type="text" name="street_streetcond[]"></td>
</tr>
<tr>
<td><input type="text" name="cep_streetcond[]"></td>
<td><input type="text" name="street_streetcond[]"></td>
</tr>
<tr>
<td><input type="text" name="cep_streetcond[]"></td>
<td><input type="text" name="street_streetcond[]"></td>
</tr>
</tbody>
// JQUERY
$(document).ready(function() {
var cep_cond = $("input[name='cep_streetcond[]']");
var rua_cond = $("input[name='street_streetcond[]']");
cep_cond.blur(function() {
var cep_replace = $(this).val().replace(/\D/g, '');
rua_cond.val("Buscando...");
$.getJSON("https://viacep.com.br/ws/" + cep_replace + "/json/?callback=?", function(dados) {
if (!("erro" in dados)) {
$("input[name='cep_streetcond[]']").next().val(dados.logradouro);
} else {
alert("CEP não encontrado");
}
});
});
});