Search code examples
jqueryhrefselector

Jquery multiple 'a' selectors with .not causing "Syntax error, unrecognized expression"


For a "click" function with jquery 1.8 i have this code

  $(document).ready(function(){

    var siteURL = "http://http://stackoverflow.com";
      
    var $internalLinks = $("a[href^='"+siteURL+"'], a[href^='/'], a[href^='./'], a[href^='../'], a[href^='#'], .loader[onclick]:not(#external)");

    var $excludeIntern = $('a[target=_blank], a[href*="vcf"], .no-ajaxy, a[href*="wp-login"], a[href*="wp-admin"], a[href*="gif"]');
        			
    $internalLinks = $internalLinks.not($excludeIntern);

    $internalLinks.live('click', function(event){alert("ok");})
                        
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
	 <a href="#test">link1</a>
    <a href="/test">link2</a>
    <a href="/test" target="_blank">link3</a>
    <a href="http://google.de">link4</a>

which causes a:

"Syntax error, unrecognized expression".

Without the .not selector it's working, but i need to exclude some links from this function.

Does anybody know why?


Solution

  • The .live() method is deprecated as of jQuery version 1.7. Try using .on() instead to attach event handlers to matched elements:

     $internalLinks.on('click', function(event){
       alert("ok");
     });
    

    Demo