0

Can anyone tell me the difference between the objects that the following lines return?.

The first line I know returns the Element Object with ID = "divID". I thought that the second did the same but the resulting objects act differently from one another..

In other words I thought that domObject was equivalent to jqObject...

var domObject = document.getElementById("rowID");
var jqObject = $("#rowID");
kpmDev
  • 1,330
  • 1
  • 10
  • 28

4 Answers4

1

The difference is that one is a jQuery object, and the other is not. jQuery objects have their own methods.

However, they are intraconvertible. You can extract a single DOM element from the jQuery object by using .get() or just [number]:

var domObject = $jqObject[0];

To do the reverse, just wrap it in the jQuery function:

var $jqObject = $(domObject);
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
0

The first returns a DOMElement using the browser's built-in methods. - Javascript

The second returns a jQuery object with that row as its only member. You can use jQuery functions on the object to manipulate it. - Jquery

pj013
  • 1,393
  • 1
  • 10
  • 28
0

var domObject = document.getElementById("rowID");

This will return an object which exposes the native javascript API (as defined by the current ECMAScript Specification) and the DOM API (details available at w3.org).

var jqObject = $("#rowID");

This will return an object which exposes the jQuery API. jQuery creates an object with a set of functions available. It also uses an indexed array on that object which holds the set of matched elements to the selector. The selector used matches css selectors, in this case matching the id "rowID", if it were to match a class, '.rowClass', then it will return a set of elements in the array matching that class. These elements can be accessed via index on the jquery object, jqObject[0] being the first, [1] the second (if present) etc.

Extracting the element in this fashion will return the native element. This native element can be referenced in many different ways. For example, it is usually assign to the this variable.

$("#rowID").each(function(){
 var nativeElement = this;//same as domObject
});

or

$("#rowId").click(function(){
 var nativeElement = this;//same as domObject
});
Travis J
  • 81,153
  • 41
  • 202
  • 273
-1

They should be the same, the problem could be that you do not have quotes around #rowID

Try this: var jqObject = $("#rowID");

Brad Faircloth
  • 337
  • 1
  • 7