views:

223

answers:

2

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. :(

+2  A: 

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++);
Pointy
Brilliant. I didn't know that existed... where is the value being stored actually? The deal with the ID's is they sort of already have ID's in this development phase which I am not ready to remove yet due to some backwards compatibility issues.
Jasconius
Accepting this for now but I am always open to more ideas. But data() seems to do the trick.
Jasconius
Oh well if the objects have "id" values, why not just use those? Note that you need to be making sure that all the "id" values are unique anyway, or else strange and mysterious (generally bad) things will start happening.
Pointy
Because, unfortunately, there are duplicate ID's on the page. Yeah, I know. Don't look at me, it was there when I got hired. Can't change it either. #arcanebusinessrules
Jasconius
Well if there are duplicate "id" values, all bets are off. A wide variety of very weird things may end up happening on your page, and until you fix that you're in the Twilight Zone.
Pointy
+1  A: 

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?

Kind Regards --Andy

jAndy
No. Selecting by ID is not an option, either.
Jasconius
He means that with your jQuery object already loaded with a selector, that $()[0] will give you the element(s) found using the given selector. Example: $(this)[0] == this returns true, while $(this) == this returns false.
Bob
indeed, $("#object")[0] returns the native HTML DOM Element.
jAndy
ah ha. Well, that's good info.
Jasconius