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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:47:15+00:00 2026-05-24T00:47:15+00:00

I have an <a> element. I then have an associated jQuery object $(a) which

  • 0

I have an <a> element. I then have an associated jQuery object $("a") which I would like to modify. So I try the following:

$("a").myAttribute = 10;
alert($("a").myAttribute);    // <--- Alerts undefined

What is the problem?

  • 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-24T00:47:16+00:00Added an answer on May 24, 2026 at 12:47 am

    Each use of $(‘a’) returns a different instance of an object. While your original code won’t work, you should be able to do:

    var a = $('a');
    a.myAttribute = 10;
    
    alert(a.myAttribute.toString());
    

    If you actually want to add the value to the DOM so that you can use it later, the “correct” way would be to use either the attr method (if you’re setting a valid HTML attribute) or the data method (if you want to use a non-standard name):

    // valid HTML attribute
    $('a').attr('name', 'someName');
    
    // non-standard name
    $('a').data('myAttribute', 10);
    

    And then later:

    alert($('a').attr('name'));
    

    Or:

    alert($('a').data('myAttribute'));
    

    If you need something more complex (like storing a complex object rather than a simple attribute/data value), I would consider writing a simple jQuery Plugin which extends the jQuery object and ads the storage/retrieval methods required for your custom object.

    To get started with a simple example, check out jQuery’s plugin authoring page:

    Plugins/Authoring – jQuery

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

Sidebar

Related Questions

I want to associate a JavaScript object with an HTML element. Is there a
I have HTML similar to the following in my page <div id=someDiv> <img src=foo.gif
I'm trying to achieve the following functionality. Within a form I have multiple fields
I'm new to JQuery and don't have a huge background in Javascript, either -
Within document.ready, I store a selector in a variable (which I assume once evaluated,
I'm making a page which has some interaction provided by javascript. Just as an
As you most likely already know, it's simple in JQuery to select all elements
i'm building a jquery slideshow carousel for a theatre site, it's being used to
Ok here goes: I have a SELECT drop down option with US state names
I am building a page that will animate objects (image/shape/div) and float them around

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.