Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 911529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:13:20+00:00 2026-05-15T17:13:20+00:00

<div id=test></div> <script> $(document).ready(function() { alert($(‘#test’).id); }); </script> Why doesn’t the above work, and

  • 0
<div id="test"></div>
<script>
  $(document).ready(function() {
    alert($('#test').id);
  });  
</script>

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:13:21+00:00Added an answer on May 15, 2026 at 5:13 pm

    The jQuery way:

    $('#test').attr('id')
    

    In your example:

    $(document).ready(function() {
      console.log($('#test').attr('id'));
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="test"></div>

    Or through the DOM:

    $('#test').get(0).id;
    

    or even :

    $('#test')[0].id;
    

    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

    $('#test').prop('id')
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use a background image instead, like this: <a style="background-image: url(avatar24x24.jpg);… May 17, 2026 at 1:47 am
  • Editorial Team
    Editorial Team added an answer Modified from MSDN. Note comments about declaration and cleanup of… May 17, 2026 at 1:47 am
  • Editorial Team
    Editorial Team added an answer If you knew that the length of conditions you would… May 17, 2026 at 1:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

What's wrong with the below? <html> <body> <div id='test'>28</div> </body> </html> <script> $(document).ready(function (){
I have the following <script> $(document).ready(function() { $(.mapLink).click(function(){ pos = $(this).attr(id); alert(pos); }); });
I cannot target an element in jQuery. First example which works : $(document).ready(function() {
Arrg. I can't spot the endless loop in this contact form. The thanks div
I am trying to make a bookmarklet that opens a popup window. Inside this
I found this site/tutorial/demo from another question here on SO. Very nice and clean
Hey i have a div moving via a jquery animation and i am trying
I'm trying to test out html5 localStorage feature. For some reason, whenever I try
This has been driving me nuts for a few days. I am desperate for
I have an IE bug that I'm not sure how to fix. Using jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.