Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: unrecognized expression

$(document).ready(function(){

    var page = window.location.hash;
    if(page != ""){
        $('a[href='+ page +']').addclass('selected');
        pageload(page.replace('#/page/', 'pages/?load='));
    }

    $('#top a').click(function(event){  
        $('#top a').removeClass('selected');
        $(this).addClass('selected');

        pageload($(this).attr('href').replace('#/page/', 'pages/?load='));

        event.preventDefault;
    });
});

 

<div id="top">
    <a href="#/page/link">Link</a>
    <a href="#/page/link">Link</a>
    <a href="#/page/link">Link</a>
    <a href="#/page/link">Link</a>
    <a href="#/page/link">Link</a>
</div>

So when i'm trying to do this, and load up a page using the window.location.hash, i get an error in the console saying:

Uncaught Error: Syntax error, unrecognized expression: [href=#/page/link]

How can i make this work?

like image 702
Thew Avatar asked Feb 07 '12 16:02

Thew


1 Answers

Try this instead:

$('a[href="'+ page +'"]').addClass('selected');

(You need to escape the value of the href – with this, you get a[href="#/page/link"].)

like image 192
Sophie Alpert Avatar answered Oct 26 '22 09:10

Sophie Alpert