Skip to content Skip to sidebar Skip to footer

Attach Data Or Array Index To Anchor Tag

I have some javascript/jquery code that dynamically populates an unordered list with a bunch of list items. In the list items I have a link and I want to associate some data with

Solution 1:

You can use the jQuery.data function to attach a "thing" to each of your links. Right after the var aSel = line:

$('a', aSel).data('thing', thing1); // Select the anchor tag inside of 
                                    // aSel and attach thing1 to it

Then when the anchor tag is clicked, retrieve it with: var thing = $(this).data('thing');


Solution 2:

You can use HTML5 Custom Data Attributes (data-*) to achieve what you want.

Take special look at Using data- attributes with JavaScript.


Post a Comment for "Attach Data Or Array Index To Anchor Tag"