<div id="test"></div>
<script>
$(document).ready(function() {
alert($('#test').id);
});
</script>
Why doesn’t the above work, and how should I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The jQuery way:
In your example:
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 functionalityan alternative for DOM selector in jquery is
which is different from
.attr()and$('#test').prop('foo')grabs the specified DOMfooproperty, while$('#test').attr('foo')grabs the specified HTMLfooattribute and you can find more details about differences here.