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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:52:22+00:00 2026-05-28T01:52:22+00:00

I have a pretty straightforward (one would think) task. Pass a value to a

  • 0

I have a pretty straightforward (one would think) task.

Pass a value to a popup window and based on a click (proceed or cancel) perform an action using the value passed.

    // on click we pass the value to the function testing - so far so good
$('#numbers').delegate('.icondelete', 'click', function(){
    testing( $(this).attr('data-options') ); 
});

I pass the value found to a function, then try to assign it to a key

function testing(id){
   //we can see that it passed successfully
    alert("here it is: " + id); 

     //push the data into key data-id
    $('#dodelete').data('data-id',id);

//but this alert doesn't return the value
alert("it should be here shouldn't it? " + $('#dodelete').attr('data-id'));
    }

html

<div id='numbers' >
<div class='icondelete'  data-options='123' >Set data-options as 123 - click here to test</div>
</div>

<br><br>
<div id='dodelete' data-id=''>landing div</div>

If you check out the fiddle http://jsfiddle.net/Microbe/cRLfC/4/ , you can see that the value is passed into the function no probs, but then when I try to assign it to the key it doesn’t.

Where am I going wrong?

  • 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-28T01:52:23+00:00Added an answer on May 28, 2026 at 1:52 am

    When using .data(), do not add the data- prefix. When reading a value back, use .data("key"), not .attr(). I’d suggest reading this and looking at the code examples.

    So your code should look like this:

    function testing(id) {
       //we can see that it passed successfully
        alert("here it is: " + id); 
    
         //push the data into key myid
        $('#dodelete').data('myid', id);
    
        //but this alert doesn't return the value
        alert("it should be here shouldn't it? " + $('#dodelete').data('myid'));
    }
    

    Settings values via .data() does not actually set an attribute/property on the object with that value. The data is actually stored inside a jQuery data structure and it is retrieved not with .attr("key"), but with .data("key").

    If you want to set an actual attribute, do that with .attr("key", value), though in jQuery coding, it’s generally better to use .data() than custom attributes.

    The only time you would use the data- prefix yourself is if you put a custom attribute in your HTML like this per the HTML5 standard for custom attributes:

    <div id="myObject" data-numloops="20"></div>
    

    Then, you could read that with this:

    var loopCnt = $("#myObject").data("numloops");
    

    When querying the data value, jQuery will first look in its own internal storage. If not found there, it will look for an HTML5 data attribute that matches the name.

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

Sidebar

Related Questions

This should be pretty straightforward I would think. I have this string: [quote=Joe Johnson|1]Hi![/quote]
What I want to do is pretty straightforward. I have a site powered by
Well, I'm gonna be pretty straightforward here, I just have a piece of code
OK, this begins to drive me crazy. I have an asp.net webapp. Pretty straightforward,
I would like to have pretty URLs for my tagging system along with all
I'm trying to do something that I think should be pretty straightforward but I
I have a PHP based web application which is currently only using one webserver
I have a pretty straightforward Excel spreadsheet, and I need to use the data
This should have been pretty straightforward using the DOM but I seem to be
I have a side project I do = in Java. It's a pretty straight-forward

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.