I received the following error in my ionic 1/angularjs cordova app. Failed to execute 'querySelector' on 'Document': '#04' is not a valid selector.
I dynamically build the id in html and then get the selected id in my controller.
<label id="{{levelIndex}}0" class="{level-selected:false}">{{vm.config.tones[0]}}</label>
My objective is to just assign a class to the selected item.
vm.selectLevelTone = function (level, tone) {
var selection = JSON.parse(localStorage.getItem('hairSelection')) || {};
selection.starting = {
level: level,
tone: tone
}
localStorage.setItem('hairSelection', JSON.stringify(selection));
vm.selectedStartLevel = config.levels[level];
vm.selectedStartTone = config.toneDesc[tone];
var levelTone = '#' + level + tone;
var note = angular.element(document.querySelector(levelTone));
note.addClass('level-selected');
$state.go('level-ending', { cId: $stateParams.cId });
};
I tried using get element by id, but that doesn't work either.
According to this post Using querySelector with IDs that are numbers, I need to escape the number. I have tried to build my id using "\" + level but I receive an error on the escape character.
I did finally get the element using the following.
var levelTone = '#' + level + tone;
In the first line I append a # to my level + tone so that integers will append, not add
In the second line, I remove the # sign (I know there must be an easier way, just don't know what it is.)
In the 3rd line, I take the advice Phil and use '[id="' + res + '"]'
Now I get an error on the note.addclass line.
var res = levelTone.substring(1, levelTone.length);
var note = document.querySelector('[id="' + res + '"]')