Graham Slick Graham Slick - 1 year ago 187
Javascript Question

jQuery selector error: unrecognized expression

I have this selector:

$('.kleo-main-header .nav > li a[href=#undefined]')

And I get an error in chrome's console:

Syntax error, unrecognized expression: .kleo-main-header .nav > li a[href=#undefined]

I tried with double quotes instead of single quotes but it didn't change. Any idea on what's causing this ?

Answer Source

You'll have to quote the attributes value when it contains special characters that are otherwise used in a selector, like the hash, which is also used for ID's.

jQuery uses a regex that searches for periods, hashes and such things in selectors, to figure out if it should search for an ID, class, or whatever.

$('.kleo-main-header .nav > li a[href="#undefined"]')

Note that you're matching an element that looks like <a href="#undefined">anchor</a> ?