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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:56:16+00:00 2026-06-12T05:56:16+00:00

I have the following code where I am setting some arbitrary data on a

  • 0

I have the following code where I am setting some arbitrary data on a li element

function showCountries( data )
{
    for( var i in data )
    {
        var li = document.createElement('li');
        $(li).html( '<a>' + i + '</a><span>&gt;</span>' );
        $(li).data('cargo', { state: data[i] });
        /* 
           right here, if I look at $(li).data('cargo').state in the debugger
           I see the data as expected
        */
        $('#countries').append( li );
        $(li).on('click', $.proxy( countrySelected, li ) );
    }
}

Later, in country selected, I want to get the cargo data. But, it is undefined

function countrySelected()
{
    var country = $(this).children('a').html();
    // country is what I expect

    var cargo = $(this).children('a').data('cargo');
    // cargo is undefined here

    // I tried this too:
    var cargo = $('#countries li.selected a').data('cargo');
    // cargo is undefined here as well
}

What I am doing wrong? Is this an issue with the .data() being set dynamically after the page is loaded?

Thanks,
Scott

  • 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-12T05:56:17+00:00Added an answer on June 12, 2026 at 5:56 am

    This line:

    var cargo = $(this).children('a').data('cargo');
    

    should be changed to this:

    var cargo = $(this).data('cargo');
    

    You set the data in the li element, not in the child a.

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

Sidebar

Related Questions

I have the following javascript function: function updatePrintCost() { var qty = parseFloat(document.getElementById('qty').value); var
I have following piece of code which posts some data into database: $post =
I have the following code which loads some html (which includes anchors) into a
I have following code for inserting data into database using PDO. It inserts data
I have following code: def whatever(url, data=None): req = urllib2.Request(url) res = urllib2.urlopen(req, data)
I have the following html code <!DOCTYPE html> <html lang=en> <head> <meta name=viewport content=width=device-width;
I have some questions regarding the following code: 1 class Test(object): 2 def __init__(self):
I have following code which have a css class called formErrorContent var promptContent =
Possible Duplicate: position: absolute without setting top/left/bottom/right? Looking at the following code, I have
I have the following code: var access_token_key, access_token_secret, consumer_key, consumer_secret, get_twitter, listen, twitter; twitter

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.