jQuery: unrecognized expression

37,440

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"].)

Share:
37,440
Thew
Author by

Thew

Hi, im Thew, 20 years old, and PHP / HTML / MYSQL developer.

Updated on July 09, 2022

Comments

  • Thew
    Thew almost 2 years
    $(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?

  • Thew
    Thew about 12 years
    "Uncaught TypeError: Object [object Object] has no method 'addclass'" this is what i get in return.
  • Thew
    Thew about 12 years
    Ah, the C in addClass needed to be capital, stupid me. Thanks!
  • Jeffz
    Jeffz almost 12 years
    good call - starting from jQ 1.5 url needs to be wrapped into something, e.g. href="my/url/" -- href=my/url/ will not go through anymore
  • Josua Marcel C
    Josua Marcel C over 11 years
    $(container+":has(ul)").length how to change that?
  • Sophie Alpert
    Sophie Alpert over 11 years
    @JosuaMarcelChrisano: What do you mean?
  • Josua Marcel C
    Josua Marcel C over 11 years
    i have the line if ($(container+":has(ul)").length === 0){...} in jquery 1.3.2, then got error when i upgrade the jquery to 1.8.2. How to upgrade my code too? the error is "Uncaught Error: Syntax error, unrecognized expression: [object Object]:has(ul)"