Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this.id vs. $(this).attr('id')

Tags:

jquery

Is

$(this).attr('id')  

the same as:

this.id 
like image 628
Phillip Senn Avatar asked Apr 08 '11 15:04

Phillip Senn


People also ask

What is attr in jQuery?

jQuery attr() Method The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element.

How can use attr ID in jQuery?

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to get or set the ID attribute value of an element. The following example will display the ID of the DIV element in an alert box on button click.

What is this ID in jQuery?

$(this) is a jQuery object that is wrapping the DOM element this and jQuery objects don't have id properties. You probably want just this.id to get the id attribute of the clicked element.


1 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.

like image 101
Jeff Avatar answered Oct 01 '22 20:10

Jeff