Ali Khakpouri Ali Khakpouri - 10 months ago 53
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.


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!


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

Answer Source

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.