Skip to content
Advertisement

How to get a DOM Element from a jQuery selector?

I’m having an impossibly hard time finding out to get the actual DOMElement from a jQuery selector.

Sample Code:

JavaScript

and in another piece of code I’m trying to determine the checked value of the checkbox.

JavaScript

And please, I do not want to do:

JavaScript

That gets me around the checkbox, but other times I’ve needed the real DOMElement.

Advertisement

Answer

You can access the raw DOM element with:

JavaScript

or more simply:

JavaScript

There isn’t actually a lot you need this for however (in my experience). Take your checkbox example:

JavaScript

is more “jquery’ish” and (imho) more concise. What if you wanted to number them?

JavaScript

Some of these features also help mask differences in browsers too. Some attributes can be different. The classic example is AJAX calls. To do this properly in raw Javascript has about 7 fallback cases for XmlHttpRequest.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement