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 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 load content via Ajax and then I'll have element like $('.my_content_class') and I
I have an element with an onclick method. I would like to activate that
I am trying to have an element fade in, then in 5000 ms fade
I have a script that create a new div element. Then, I want to
OK, I have a list ( <ul> ) then inside each <li> element I
I have an array which i need to sort its elements by occurrence then
I have an image with an associated image map (below) and I'm using jQuery
I have a simple .Net enum. I also have a view model object which
So I have a form and I'll have error messages associated with each input/element.
I have made a string vector vector<string> actor_; and then added elements in it

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.