1

Being curious. jQuery is written on top of JavaScript. So, for a given selected DOM element, does jQuery keep a property (attribute) that acts as a handle to the corresponding (internal) JavaScript DOM object? If so, what property in the jQuery object acts as a handle to the corresponding JavaScript object.

To facilitate further, I have quickly written an example at jsfiddle: http://jsfiddle.net/mMvaD/1/ . The example basically retrieves a DOM object using both jQuery and prints its (enumerable) properties. Could someone point me to the property in the jQuery object points to a JavaScript object, if we have one? For the sake of completeness, I have also shown properties belonging to a corresponding JavaScript object.

Here is the code:

<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<body>

<div id="idDiv"></div>
<script type="text/javascript">
$(document).ready(function() {
    $("body").append("jQuery Object:<br>");
    elem1 = $("#idDiv");
    for (var item in elem1) {
         $("body").append(item + ", ");
    }

    $("body").append("<br><br>JavaScript Object:<br>");
    elem2 = document.getElementById("idDiv");
    for (var item in elem2) {
         $("body").append(item + ", ");
    }
});
</script>
</body>
</html>
Manoj Pandey
  • 4,528
  • 1
  • 17
  • 18
  • 2
    Duplicate of [How to get a DOM Element from a JQuery Selector](http://stackoverflow.com/questions/1677880/how-to-get-a-dom-element-from-a-jquery-selector) and [jQuery get DOM node?](http://stackoverflow.com/questions/2316199/jquery-get-dom-node). Those questions are amoung the first results when Googling "jquery get dom element". Please do a tiny bit of research before posting a question. – Colin Brock Sep 28 '13 at 00:46
  • Fair point.. In that case, we can go ahead and close it. – Manoj Pandey Sep 28 '13 at 01:03

3 Answers3

4

Yes!

A jQuery object is an array-like wrapper for a DOM object. elem1 in your example is one of these array-like objects with only one item.

The (perhaps) preferred way to get at elem1's DOM element would be:

elem1.get(0);

Great question! Thanks for the detail and the fiddle.

Reed Spool
  • 843
  • 7
  • 15
3

you can use index to retrieve it

elem1 = $("#idDiv")[0];

or use .get(index)

elem1 = $("#idDiv").get(0);
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2
$("body")[0];
$("body").get(0);

Those both should get the DOM object from jQuery.

benekastah
  • 5,651
  • 1
  • 35
  • 50