Graham Slick
Graham Slick

Reputation: 6870

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 ?

Upvotes: 2

Views: 402

Answers (1)

adeneo
adeneo

Reputation: 318222

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> ?

FIDDLE

Upvotes: 6

Related Questions