How to solve the Invalidcharactererror error

Asked

Viewed 30 times

1

I’m using a requisition ajax, that returns me a json, from a page .php, named after a city of Mato grosso do sul calling for: Antônio João.

The error is javascript. Which says the following:

inserir a descrição da imagem aqui

  • 3

    What code is generating this error? It matters what’s on and around the 1202 line.

  • @bfavaretto, you are the best . ONLY this simple observation helped me to identify the error. vlw!!!

1 answer

2


Some javascript functions do not allow empty spaces between characters. An example, in my case, was the add(). I’m using to add a classList

var nomediv = 'Antônio João';
element.classList.add(nomediv);

How to solve?

element.classList returns a DOMTokenList element class attributes. element is a Domtokenlist representing the elementNodeReference class attribute. Therefore, it is known that as a rule, não é permitido espaços para compor o nome de uma classe. And to solve the error was implementing a simple REGEX:

var nomediv = 'Antônio João';
element.classList.add(nomediv.value.replace(/\s/g, '') );

SOURCE

  • Using accent in a class name is also asking for trouble. Why not use the database ID instead of the city name? And why not put this as element id instead of class ?

  • They are alternative solutions and not assertive solutions to this problem, @bfavaretto.

  • At the very least, removing the accents would be a wise alternative...

Browser other questions tagged

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