最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery - object.id is undefined when it shouldn't be - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Aug 23, 2009 at 2:14 LawrenceLawrence
Add a comment  | 

2 Answers 2

Reset to default 14

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 );

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));
发布评论

评论列表(0)

  1. 暂无评论