Why doesn\'
id
is a property of an html Element
. However, when you write $("#something")
, it returns a jQuery object that wraps the matching DOM element(s). To get the first matching DOM element back, call get(0)
$("#test").get(0)
On this native element, you can call id, or any other native DOM property or function.
$("#test").get(0).id
That's the reason why id
isn't working in your code.
Alternatively, use jQuery's attr
method as other answers suggest to get the id
attribute of the first matching element.
$("#test").attr("id")