remove() with jquery id with ()

Asked

Viewed 23 times

-1

In the HTML I received there are many Divs like this (or almost like this): id-name-(24498765);

When running this in jquery:

$('#'+'nome-do-id-(24498765)').remove();

I get the following error:

jquery.js:2 Uncaught Error: Syntax error, unrecognized expression: #nome-do-id-(24498765)

I realized that the error is in () (partenteses), I know it is not right to create Ids with (), but I received the system. How to make Jquery identify Ids that have parentheses in their name?

  • name-do-id is what is actually written or is a representation ? if it is a representation, comments here a real model.

  • is a represnetation, can be any name without special character and accent, but always with parentheses: kudshf-(2333) jdskhf-dsfds-(9), etc... the problem is parentheses

1 answer

0

I managed to sort it out like this:

var nomeArquivoApaga = nomeArquivo.replace('(', '\\(');
var nomeArquivoApaga = nomeArquivoApaga.replace(')', '\\)');
  • No need to use replace, you can do it right on the selector jQuery!

Browser other questions tagged

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