views:

306

answers:

2

Hi,

I'm working with JQuery and i'm running into this strange (or perhaps stupid) error.

In my HTML I have:

<input type="password" name="repeatPassword" id="id_repeatPassword" />

And then in my javascript code i have:

validateRepeatPassword($('#id_repeatPassword'));

Unfortunately in the function "validateRepeatPassword":

function validateRepeatPassword(o) {
        // this works
        if (o.value == $("#id_password").val()) {
        // this returns "undefined"
        alert(o.id)
...
}

why?

+2  A: 

o is a reference to a jQuery object, NOT a DOM element reference. Inside your validateRepeatPassword function do:

alert( $(o).attr('id') );

If you want to access the direct DOM element's property from the jQuery object,

alert( o[0].id )

alert( o.get(0).id );
meder
o is already a jquery object
cletus
I know that, but wrapping it around inside the function would make it more lenient in that it would also accept DOM Elements in the argument. It's not a huge deal.
meder
cool, but is there a way to use jQuery to retrieve the obeject as a DOM element reference instead?
That's really not a good argument to downvote :p
meder
Yes. you can use [0] or .get(0)..alert( o[0].id )oralert( o.get(0).id )
meder
great, thanks a lot!
+1  A: 

Inside your function o is a jQuery object, you should grab the id with the attr function of of o.

alert(o.attr('id'));

But if you want to work directly with the DOM element on your validateRepeatPassword function, you can pass a reference to the element:

validateRepeatPassword($('#id_repeatPassword').get(0));
CMS