I’m building a list of links dynamically using an array, which will result in something like this:
<div>
<p class="link">label</p>
<p class="link">label</p>
<p class="link">label</p>
<p class="link">label</p>
<p class="link">label</p>
</div>
<div>
<p class="link">label</p>
<p class="link">label</p>
<p class="link">label</p>
<p class="link">label</p>
<p class="link">label</p>
</div>
Then I would have a click handler:
$(".link").click(function(){
//do something
myarray[?].stuff;
});
I want to retrieve something from an array, but I’m not sure what the best way is to reference the link. I could add the array key as an id in the p tag, however I’m wondering if there is a more elegant way of handling it.
You could use the index, but that will give you :
You could use the parents index aswell, something like :
FIDDLE
or a data attribute