Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get real object from Jquery?

How to get real object from Jquery selector result? Example:

 $("form").first().id != $("form").first().attr("id")

so this mean result somehow wrapped/delegated with jquery how to unwrap it?

like image 781
yura Avatar asked Dec 05 '10 09:12

yura


People also ask

Which method returns the element as a jQuery object?

getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object. The following figure illustrates the difference.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.

How do you object in jQuery?

Another way to make objects in Javascript using JQuery , getting data from the dom and pass it to the object Box and, for example, store them in an array of Boxes, could be: var box = {}; // my object var boxes = []; // my array $('div. test'). each(function (index, value) { color = $('p', this).

How do I get DOM element?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable. Logging demoId to the console will return our entire HTML element.


2 Answers

$("div")[0] or $("div").get(0), substituting 0 for the index of the element you want.

If you have multiple DOM elements that you want out, you can use .toArray().

like image 139
simshaun Avatar answered Oct 11 '22 06:10

simshaun


The left operand is incorrect because here:

$("form").first().id

first() returns a jQuery object, so you can't use id (a DOM element property) on it. To get the DOM element wrapped by the jQuery object you use array deferencing:

$("form")[0].id

Or get():

$("form").get(0).id

The following should evaluate to true:

$("form")[0].id == $("form").first().attr("id") 

And therefore this should be false:

$("form")[0].id != $("form").first().attr("id") 
like image 28
BoltClock Avatar answered Oct 11 '22 07:10

BoltClock