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

  • SEARCH
  • Home
  • 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

Related Questions

What's wrong with the below? <html> <body> <div id='test'>28</div> </body> </html> <script> $(document).ready(function (){
Demo page for testing : http://jsfiddle.net/rCKzs/ <script type=text/javascript> $(document).ready(function() { alert( $(#test).width() ); });
Update: I did get this working, here is the new javascript: $(document).ready(function() { $('a').click(
Why is this not working? $(document).ready(function(){ var content = '<a href=http://example.com/index.php><b>Some text!</b></a> - <a
function(a){return f.nodeName(a,iframe)?a.contentDocument||a.contentWindow.document:f.makeArray(a.childNodes) This is the offending line from IE9 script debugging I am using
Hopefully a simple issue: $(document).ready(function(){ $(#regform).validate({ errorPlacement: function (error, element) { $(element).parent().next().next().hide(); //works $(element).parent().next('div.test').hide();
<div> <div>test</div> </div> $(div:contains('test')).css('display','none'); I know I am going to kick myself on this.
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
<div style=background-color:black> <div style=float:right> Test message </div> <div> This will show 'Test message' with
I cannot target an element in jQuery. First example which works : $(document).ready(function() {

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.