How to resolve the InvalidCharacterError error

1

I'm using a ajax request, which returns me a json, from a .php page, with the name of a city of Mato Grosso do Sul named: Antônio João .

The error is javascript. Which reads as follows:

    
asked by anonymous 15.01.2016 / 23:01

1 answer

2

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

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

How to solve?

The element.classList returns a DOMTokenList of the element's class attributes. element is a DOMTokenList that represents the class attribute of the elementNodeReference. 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

    
15.01.2016 / 23:21