Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deriving an HTMLElement Object from jQuery Object

I'm doing a fairly exhaustive series of DOM manipulations where a few elements (specifically form elements) have some events.

I am dynamically creating (actually cloning from a source element) several <select> boxes and assigning a change() event to them.

The change event executes, and within the context of the event, "this" is the HTML Element Object.

What I need to do at this point however is determine a context for this HTML Element Object. I have these objects stored already as jQuery entities in assorted arrays, but obviously

[HTMLElement Object] != [Object Object]

And the trick is that I cannot cast $(this) and make a valid comparison since that would create a new object and the pointer would be different.

So... I've been banging my head against this for a while. In the past I've been able to circumvent this problem by doing an innerHTML comparison, but in this case the objects I am comparing are 100% identical, just there's lots of them. Therefore I need a solid comparison.

This would be easy if I could somehow derive the HTMLElement object from my originating jQuery object.

Thoughts, other ideas? Help. :(

like image 796
M. Ryan Avatar asked Mar 30 '10 17:03

M. Ryan


People also ask

What is a DOM object jQuery?

The Document Object Model (DOM for short) is a representation of an HTML document. It may contain any number of DOM elements. At a high level, a DOM element can be thought of as a "piece" of a web page. It may contain text and/or other DOM elements.

Which method returns the element as a jQuery object?

getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object.

Is it possible to access the underlying DOM element using jQuery?

The Document Object Model (DOM) elements are something like a DIV, HTML, BODY element on the HTML page. A jQuery Selector is used to select one or more HTML elements using jQuery. Mostly we use Selectors for accessing the DOM elements.

What is the difference between element and HTMLElement?

HTMLElements inherit from Element which inherit from Node. This means that your HTMLElement is simultaneously an 'instanceof' all three. The fact that it is an HTMLElement means it has an interface that's contains methods only an HTMLElement would need like attachEvent. Actually, Element inherits from Node .


2 Answers

This would be easy if I could somehow derive the HTMLElement object from my originating jQuery object

you don't just mean $("#object")[0] or $("#object").get(0) with 'derive' do you?

like image 129
jAndy Avatar answered Oct 22 '22 00:10

jAndy


Can't you just use $(this).data("something") to keep data on your elements and then check the values later? (That's assuming you can't just give these things plain ol' "id" values.)

Oh also jQuery itself has a "guid" element that you can use (be careful!)

$(myNewObject).data("identity", $.quid++);
like image 4
Pointy Avatar answered Oct 22 '22 00:10

Pointy