2

I have the following link:

<a href="/Packages/PackageActionDownloadAsync" data-ajax-type="Download" data-ajax-packageid="AGI-VS-GAME-M52-1.5.3.2.67" data-ajax-machineid="30" class="iconGear action tip" data-hasqtip="true" oldtitle="Download" title="" aria-describedby="qtip-1">Download</a>

and javascript code:

       var obj = $(this),
            objData = obj.data(),
            packageId = objData.ajaxPackageid,
            operation = objData.ajaxType;

I tried this:

alert(objData.ajaxPackageid);
alert(objData.ajaxPackageId);
alert(objData.AjaxPackageId);

they all return "undefined". The only one that works is this:

alert(objData.ajaxType);

what is going on? I'm using "jquery-1.7.1.js"

ShaneKm
  • 20,823
  • 43
  • 167
  • 296

2 Answers2

4

I'm suprised any of them work accessing them like that. You need to access them via string names in jQuery. Like so:

var obj = $(this);
alert(obj.data("ajax-packageid"));
alert(obj.data("ajax-type"));
alert(obj.data("ajax-machineid"));

Take a look at the jquery .data object

DEMO: http://jsfiddle.net/xFmn3/

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • @ShaneKm For a cross browser solution, you should really use the `.data("")` method. – Mathew Thompson Mar 14 '13 at 09:07
  • 1
    It seems they worked due to a quirk of the way `$.fn.data` is implemented in jQuery. See [this answer](http://stackoverflow.com/a/7262427/1852838). I was also surprised to learn you can access them this way. – jeteon Aug 20 '15 at 13:23
2

You need to pass the key for the data you want to receive to the data() function.

   var obj = $(this),
   packageId = obj.data("ajax-packageid"),
   operation = obj.data("ajax-type");

Working Example: http://jsfiddle.net/sUSCe/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189