1

Simply dont understand why jquery .click() cannot be executed, cause everytime the element is fired I got an error you have seen in the title.

I have an element which is linked to my document by name B Group and I'm calling to that element like $('a[href="#B Group"]'). As you can see it has a space inside. Is it a misunderstanding for me or there cant be spaces inside?

my code

$(document).on('click', '.groups-activity .glyphicon-list',
        function () {
            var target = $(this).parents('tr');
            var name = target.find('td').slice(3, 4).text().trim();
            if ($('a[href = "#' + name + '"]').length > 0) {
                $('a[href="#' + name + '"]').click();
                return 0;
            }
            activityGroups.push(name);
            $('.tabs').append('<li class="groups tab col s3"><a data-admin="false" class="data-tab" href="#' + name + '">' + name + '</a><span class="close-tab">&times;</span></li>');
            $('a[href="#' + name + '"]').click();
            $.ajax({
                type: 'POST',
                url: '/Admin/CheckArray',
                data: { activityGroups: activityGroups }
            });
        });
Lab Lab
  • 781
  • 10
  • 34
  • Possible duplicate of [jquery IDs with spaces](https://stackoverflow.com/questions/596314/jquery-ids-with-spaces) – Asons Apr 18 '18 at 13:29
  • well it is inside a `href` property, indicating it should be a link. I believe JS is just HTML encoding it by changing the space to its HTML urlencode character `%20`, maybe that is why its not working – N. Ivanov Apr 18 '18 at 13:30
  • @N.Ivanov so is there a way to disable urlencoding? – Lab Lab Apr 18 '18 at 13:32
  • @LabLab I guess you can simply use [decodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) – N. Ivanov Apr 18 '18 at 13:44
  • @N.Ivanov didn't work. jQuery automatically encodes text, so I can't pre-encode my `B Group`, because its simply text – Lab Lab Apr 18 '18 at 14:01
  • 1
    well then do not uses spaces in IDs – N. Ivanov Apr 18 '18 at 14:02
  • @N.Ivanov this is a unique name that I receive from database, so the space should be there – Lab Lab Apr 18 '18 at 14:09
  • Are you sure the `name` variable contains specified value? Appositely, you missed the `#` in `$('a[href="' + name + '"]').click();` line. – Alexander Apr 18 '18 at 14:27
  • @Alexander yeap, im sure, thats an accident. I'll edit my answer's code – Lab Lab Apr 18 '18 at 14:52

0 Answers0