Is

$(this).attr('id') 

the same as:

this.id 

3 Answers

No, they're not exactly the same.

They'll both return the element's ID, but if the element has no ID, then this.id will return a blank string while $(this).attr("id") will return undefined.

Almost (see Jeff's answer).

jQuery abstracts away attribute getting, but it isn't always the most terse option.

It is however shorter than getAttribute('id').

Same result, but this.id is much faster as it doesn't require all the jQuery stuff around it. You will also get different results if that item doesn't have an id.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy