Take the 2-minute tour ×
<div id="test"></div>

$(document).ready(function() {

Why doesn't the above work, and how should I do this?

share|improve this question
add comment

7 Answers

up vote 253 down vote accepted

The jQuery way:


In your example:

<div id="test"></div>

$(document).ready(function() {

Or through the DOM:


or even :


and reason behind usage of $('#test').get(0) in JQuery or even $('#test')[0] is that $('#test') is a JQuery selector and returns an array() of results not a single element by its default functionality

an alternative for DOM selector in jquery is


which is different from .attr() and $('#test').prop('foo') grabs the specified DOM foo property, while $('#test').attr('foo') grabs the specified HTML foo attribute and you can find more details about differences here.

share|improve this answer
It amazes me each time that jQuery does not have a shortcut for this like $('#test').id(). –  awe Apr 2 at 16:33
It would be rarely useful because id's are typically hard-coded into the HTML and JS. When you write JS, you already know the ID of some element, so you write that ID to retrieve the element. You rarely need to get the ID of an element programmatically. –  daniel1426 Apr 30 at 18:07
rarely? this question has 112106 views :-) –  Simon_Weaver May 10 at 21:55
add comment

$('selector').attr('id') will return the id of the first matched element. Reference.

If your matched set contains more than one element, you can use the conventional .each iterator to return an array containing each of the ids:

var retval = []
return retval

Or, if you're willing to get a little grittier, you can avoid the wrapper and use the .map shortcut.

return $('.selector').map(function(index,dom){return dom.id})
share|improve this answer
BTW, I think retval.push($(this).attr('id')) can be written retval.push(this.id) –  Darren Cook Mar 31 at 13:26
add comment

id is a property of an html Element. However, when you write $("#something"), it returns a jQuery object that wraps the DOM element. To get the native DOM element back, call get(0)


On this native element, you can call id, or any other native DOM property or function.


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.

share|improve this answer
+1, this helped me understand Why –  DelightedD0D Apr 7 at 1:37
add comment

Above answers are great, but as jquery evolves.. so you can also do:

var myId = $("#test").prop("id");
share|improve this answer
add comment

.id is not a valid jquery function. You need to use the .attr() function to access attributes an element possesses. You can use .attr() to both change an attribute value by specifying two parameters, or get the value by specifying one.


share|improve this answer
add comment

$('#test') returns a jQuery object, so you can't use simply object.id to get its Id

you need to use $('#test').attr('id'), which returns your required ID of the element

This can also be done as follows ,

$('#test').get(0).id which is equal to document.getElementById('test').id

share|improve this answer
.. and also $('#test')[0].id which is the same as .get(0) –  Gaby aka G. Petrioli Jul 13 '10 at 17:24
add comment

Using above code you can get id.

share|improve this answer
add comment

Your Answer


