Does anyone know the possible cause of the "Uncaught Error: Syntax error, unrecognized Expression" error?

Asked

Viewed 749 times

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:

inserir a descrição da imagem aqui

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, try codigo_empr.value

  • Just one question, my question is misspelled to the point of being negative ?

  • 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.

  • 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.

  • 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 for codigo_empr.value have solved, otherwise edit the question.

  • The code_empr is returning exactly the numbering I needed. When I give console.log the numbering present in code_empr appears to me.

  • I added in the question description the creation code of the button related to the event.

  • 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.

  • When I do this, the code_empr variable is empty. No value is assigned to it.

  • there’s something really wrong there, it wasn’t supposed to happen.

  • 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

  • You see, @Guilhermenascimento , the error is not so evident so. ;/

  • I get scared to put big code in the question and the guys complain.

  • 1

    @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.

Show 9 more comments

2 answers

1


Analyzing your code, I could notice that the error is in typing the name of the variable "codigo_empr" in the lines marked below.

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');

On the two lines I entered with asterisks, you used the name "codigo_emp" that was not declared or initialized at any time in your code. Check and see if it solves.

  • Put, Denied. That’s right. I think I had copied this excerpt from the code from another place and, as the variable has the similar name, I just missed it. Silly error. I since yesterday looking for the error in the whole code. The variables, in this part of the code, had to be all code_empr. Thank you very much!

0

the error is in the way that is caught the value of the variable pegar_status

if input

let pegar_status = $(pegar_tr).val();
                                ↑

if it is another type of element use

let pegar_status = $(pegar_tr).data('value');
                                ↑

for this second way, you will need to set the 'date' like this

<elemento data-value="valor">
           ↑
  • tomorrow I’ll try it your way. But explain one thing to me. Why can’t I pick up by attr('name') ? And another thing, when I give a console.log in the variable pega_status, appears the value I want stored in it. That is, the name is being caught like that. I know that you can use date-attribute, but I do not know the problem in using the name.

  • The attr function is being depreciated, this may be the error, try with . prop('abc')

  • I changed it and it didn’t work, but I don’t think that’s the problem, William, because attr works in the whole code. I believe the error is in the part of the code where I will mark there in the question.

Browser other questions tagged

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