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 9155177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:32:50+00:00 2026-06-17T12:32:50+00:00

I have a DOM element of class bar that stores data foo , which

  • 0

I have a DOM element of class bar that stores data foo, which is the id of another DOM element.

<div class="bar" data="foo">...</div>

When I hover over such element, I want to get the DOM element that the data (id) points to. I did this:

$(".bar").hover(function(){alert($("#"+$(this).data()));});

But when I hover, I get this error:

Uncaught Error: Syntax error, unrecognized expression: #[object Object]

What am I doing wrong, and how can I fix it?

  • 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-06-17T12:32:51+00:00Added an answer on June 17, 2026 at 12:32 pm

    You are confusing the data() method with the element’s data attribute.

    The data() method returns an unrelated object of data attached to the element and not the string you are looking for.


    Solution 1 – data as an attribute

    To get the value you’re looking for in your example, you should query the attribute with the attr() method:

    $(".bar").hover(function(){alert($("#"+$(this).attr('data')));});
    

    Solution 2 – data as an object

    Using the data() method you can manually attach information to the element:

    $('.bar').data('data','foo');
    $(".bar").hover(function(){alert($("#"+$(this).data('data')));});
    

    Note how in this case 'data' is just an arbitrary name for a key in the element’s data object. For more on the data() method see the jQuery manual


    Solution 3 – data as an object and HTML5 attribute

    I believe your original intention was to query the data attribute as an HTML5 data-attribute, that according to the jQuery documentation should be automatically pulled in to jQuery's data object.

    However, note that according to the HTML5 specification, HTML5 data-attributes are expected to have a data- prefix. So in order for your example to work, the attribute cannot be named data but rather data-something. For example:

    <div class="bar" data-something="foo">...</div>
    

    Which you can then access with jQuery’s data() method:

     $(".bar").hover(function(){alert($("#"+$(this).data('something')));});
    

    Note how data-something is accessed with data('someting'), since jQuery automatically removes the data- prefix.

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

Sidebar

Related Questions

I have this html code: <html> <head> ... </head> <body> <div> <div class=foo data-type=bar>
I have the following function to remove a DOM element div, $('#emDiv').on(click, ':button[data-emp-del=true]', function
Assuming I have HTML similar to this: <div class=fooContainer> <div class=barContainer> <a href=#>foo-bar</a> </div>
I have a class which creates a DOM element and has to capture all
I have a DOM element elem that has an ancestor element which has the
I have a DOM element in memory that is yet to be injected in
Say I have the following DOM element in my piece of code: <div id=test>
I have a function that changes a dom element's text to a given value,
I have backbone view that binds an action to an element in the DOM.
If I have the following DOM elements <div class=item>content1</div> <div class=item>content2</div> how, 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.