Ali Khakpouri Ali Khakpouri - 6 months ago 40
HTML Question

jQuery Selector returns function instead of DOM

This is an odd one..

The followings are taking place well after the page has been loaded.
I'm trying to add a class to a known (I know the element's id) DOM element.

$('#day-08-07-2016')


However, this returns back a jQuery function, not the DOM element.
enter image description here

Since the selector is an Id and there is only one element on the page, i can't use the [0] or first() functions.

Ultimately, the followings doesn't work!

$('#day-08-07-2016').addClass('active')


The element clearly exists on the page.
enter image description here
What am I doing wrong?

Answer

The title of your question isn't really related. jQuery selectors always return a deferred function of sorts. Commonly called a jQuery set wrapper. In this case, it's wrapping an empty set.

I think your real confusion here is caused by the # that you put in the id. Get the # out of the id and you'll be fine. Alternatively, see other answers on escaping it.