0
I have a code in jQuery that I hold an event when I click a certain button. The problem is that when this event runs, the console shows me the following error:
That a. stand-by-column is an 'a' element that I have inside a 'tr'. These elements are created dynamically. I will leave the code of the event that causes the error. I don’t know if the mistake is exactly in him.
Event code: (I think this code is expendable to solve the question. But I posted a part of the event, because that’s when the error occurs)
//CÓDIGO PARA QUANDO CLICAR NO BOTÃO 'REABRIR ATIVIDADES' DA COLUNA REABRIR, ENTÃO A ATIVIDADE SERÁ REABERTA
$(document).on("click", ".reabrir-atividade-coluna", function(e) {
let pegar_tr = $(this).parent().parent();
let pegar_status = $(pegar_tr).attr('name');
console.log(pegar_status);
var elemento = $(this);
var sts = $(this).attr('name');
var codigo_empr = $(this).attr('value');
//VERIFICANDO O STATUS DO CAMPO QUE RECEBERÁ O NOVO STATUS APÓS O CLIQUE NO REABRIR
//PASSAR BOTÃO DE CONCLUIDO PARA INICIADO
if (sts == "CONCLUIDO") {
if (data_banco_vencimento >= data_hoje_formatada) {
$("tr." + codigo_empr + " button.reabriratividade").html("INICIADO");
$("tr." + codigo_empr + " button.reabriratividade").removeClass('CONCLUIDO')
$("tr." + codigo_empr + " button.reabriratividade").removeClass('btn-success')
$("tr." + codigo_empr + " button.reabriratividade").addClass('INICIADO');
$("tr." + codigo_empr + " button.reabriratividade").addClass('btn-info');
//Altera o atributo name do elemento
elemento.attr('name', 'INICIADO');
var novo_status = "INICIADO";
$("tr." + codigo_emp + " a.stand-by-coluna").attr('data-status', 'INICIADO');
var reabrir = reabrirAtividade(sts, novo_status, codigo_empr);
sts = elemento.attr('name');
//ACREDITO QUE POSSA ESTAR ACONTECENDO UM POSSÍVEL ERRO AQUI
$(this).parent().parent().attr('name', 'INICIADO');
// TRECHO ACIMA
$(this).attr('name', 'INICIADO');
} else {
$("tr." + codigo_empr + " button.reabriratividade").html("INICIADO_VENCIDO");
$("tr." + codigo_empr + " button.reabriratividade").removeClass('CONCLUIDO')
$("tr." + codigo_empr + " button.reabriratividade").removeClass('btn-success')
$("tr." + codigo_empr + " button.reabriratividade").addClass('INICIADO_VENCIDO');
$("tr." + codigo_empr + " button.reabriratividade").addClass('btn-warning');
//Altera o atributo name do elemento
elemento.attr('name', 'INICIADO_VENCIDO');
var novo_status = "INICIADO_VENCIDO";
$("tr." + codigo_emp + " a.stand-by-coluna").attr('data-status', 'INICIADO_VENCIDO');
var reabrir = reabrirAtividade(sts, novo_status, codigo_empr);
sts = elemento.attr('name');
//ACREDITO QUE POSSA ESTAR ACONTECENDO UM POSSÍVEL ERRO AQUI
$(this).parent().parent().attr('name', 'INICIADO_VENCIDO');
//TRECHO ACIMA É O POSSÍVEL ERRO
$(this).attr('name', 'INICIADO_VENCIDO');
}
}
});
Code creating the element that is displaying the error (it is inside a loop).
//botão stand by
atividades += '<td style="padding:0;text-align:center;" class="th-ocultar-responsivo-tbody td-padding">'
atividades += '<a type="button" data-toggle="modal" data-target="#modal-stand-by" data-placement="top" class="btn btn-sm btn-light css-stand-by-coluna stand-by-coluna" data-status="' + valor.STATUS + '" id="' + valor.codigo + '" value="' + valor.COD + '">'
atividades += '<i class="text-dark fas fa-exclamation-circle"></i>'
atividades += '</a>'
atividades += '</td>';
Code creating the element on which I click to occur the event
atividades += '<td style="padding:0;text-align:center;" class="th-ocultar-responsivo-tbody td-padding">'
atividades += '<a type="button" data-toggle="tooltip" data-placement="top" title="Voltar o Status da atividade para o estado anterior." class="btn btn-sm btn-light css-reabrir reabrir-atividade-coluna" name="' + valor.STATUS + '" value="' + valor.codigo + '">'
atividades += '<i class="text-info fas fa-history"></i>'
atividades += '</a>'
atividades += '</td>';
I did not put more code, as they could complain the description was too long. Thanks in advance!
codigo_empr
is returning an element and not the value of this element, trycodigo_empr.value
– Guilherme Nascimento
Just one question, my question is misspelled to the point of being negative ?
– Gato de Schrödinger
I don’t know why, only the person who was negative could inform you, but I suppose it could be for innumerable reasons, for example, instead of copying the error you put a print, what is a huge exaggeration, otherwise, the error is a little obvious and you just put an excerpt of the code, waiting for us to guess the source of the problem. That’s why in the site’s HELP you talk about both MCVE and creating a minimal, reproducible example of the error. I hope it helps you understand. PS: I voted to close, don’t take this the wrong way, but your question is typo, even so I made an effort to resolve in the first comment.
– Guilherme Nascimento
I did not know about the error print issue. I thought only the code print was 'bad formulation of the question'. I did not know that the error was evident. At least for me it isn’t. I put the excerpt of the event code that presents the error, not to leave the description of the code too big (I didn’t expect you to guess the origin, I just wanted to avoid excesses). I will try to do what you said. Thanks for the advice, on the next question I try to elaborate better.
– Gato de Schrödinger
I understand that you did not understand and so I commented the possibility, precisely because I think it important to guide :) I hope the first comment I made about exchanging
codigo_empr
forcodigo_empr.value
have solved, otherwise edit the question.– Guilherme Nascimento
The code_empr is returning exactly the numbering I needed. When I give console.log the numbering present in code_empr appears to me.
– Gato de Schrödinger
I added in the question description the creation code of the button related to the event.
– Gato de Schrödinger
Change
$(this).attr('value');
for$(this).val();
and see if you’re not using that var with something else, 'cause I think you’re mixing it up.– Guilherme Nascimento
When I do this, the code_empr variable is empty. No value is assigned to it.
– Gato de Schrödinger
there’s something really wrong there, it wasn’t supposed to happen.
– Guilherme Nascimento
hello, I do not think it is a good idea to change the name of the element, should use a data-attibute for this if you want to keep a value that can change
– Ricardo Pontual
You see, @Guilhermenascimento , the error is not so evident so. ;/
– Gato de Schrödinger
I get scared to put big code in the question and the guys complain.
– Gato de Schrödinger
@Petherson, just what I said, is missing an excerpt from the code and the part I said that is clear is that an HTML element is being passed instead of its value. That’s why I cited the MCVE (how to create a minimal executable example) that is quoted in the help of the site, of course that after your Ditto the question improved, but before that, at the time I commented the first time, I still had no code clarity. I’m just guiding you.
– Guilherme Nascimento