.data() gets the first list item's data attribute only?

Ben Racicot

Why is this code repeatedly getting the first data attribute only?

HTML

<ul class="als-wrapper">
    <li class="als-item"><a data-loc-subject="test value"><img src="http://www.ikea.com//us/en/images/products/pugg-wall-clock__13080_PE040801_S4.jpg" height="100" width="100"/></a>test value</li>
    <li class="als-item"><a data-loc-subject="a second value"><img src="http://www.ikea.com//us/en/images/products/pugg-wall-clock__13080_PE040801_S4.jpg" height="100" width="100"/></a>a second value</li>
    <li class="als-item"><a data-loc-subject="a third value"><img src="http://www.ikea.com//us/en/images/products/pugg-wall-clock__13080_PE040801_S4.jpg" height="100" width="100"/></a>a third value</li>
</ul>

jQuery

$( ".als-item" ).click(function(e) {
    e.preventDefault();

    var data = $('.als-item a').data('loc-subject');
    alert(data);
});

My jsfiddle example.

Sushanth --

You would need to use the this context and get the data attribute for the anchor inside each list element.

Instead of

var data = $('.als-item a').data('loc-subject'); 

try

var data = $(this).find('a').data('loc-subject');

Check Fiddle

And use console.log for debugging purpose instead of an alert statement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript active class only gets applied to first list item

From Dev

Ajax get data only first item in while loop

From Dev

data-sly-list how to check for first item

From Dev

Isotope sort item by data attribute

From Dev

Find and delete an item by data attribute

From Dev

Find and delete an item by data attribute

From Dev

Jquery tablesorter only sort first page's data only

From Dev

Select first element with data attribute

From Dev

Only first element prints, when printing linked list data

From Dev

UDP client gets only beginning of data (Java)

From Dev

Horizontal list show item only if it gets displayed fully

From Dev

Sorting a list by data-attribute

From Dev

Sorting a list by data-attribute

From Dev

How to make a second list populate after a you select an item in the first list preferably from a data base/excell

From Dev

Select item with comma separated data-attribute

From Dev

Conditional attribute with data_bag_item in Chef?

From Dev

Select item with comma separated data-attribute

From Dev

shopping list item data type

From Dev

ASP.NET repeater only renders the first item in my XML data source

From Dev

Get Data Attribute of First Child Element with jQuery

From Dev

HTML list not showing bullet point on FIRST list item ONLY

From Dev

Pass first item of subscribed data to service

From Dev

Only get part out of data-attribute

From Dev

Form just gets data from first row in table

From Dev

getting only positive number from a list that containing heterogeneous data type item in python 3

From Dev

cloning & deleting list item but prevent deletion of first if it's the item

From Java

RecyclerView set the last ArrayList item data to the first item

From Dev

RecyclerView set the last ArrayList item data to the first item

From Dev

AJAX send all data but controller gets only part of it

Related Related

  1. 1

    Javascript active class only gets applied to first list item

  2. 2

    Ajax get data only first item in while loop

  3. 3

    data-sly-list how to check for first item

  4. 4

    Isotope sort item by data attribute

  5. 5

    Find and delete an item by data attribute

  6. 6

    Find and delete an item by data attribute

  7. 7

    Jquery tablesorter only sort first page's data only

  8. 8

    Select first element with data attribute

  9. 9

    Only first element prints, when printing linked list data

  10. 10

    UDP client gets only beginning of data (Java)

  11. 11

    Horizontal list show item only if it gets displayed fully

  12. 12

    Sorting a list by data-attribute

  13. 13

    Sorting a list by data-attribute

  14. 14

    How to make a second list populate after a you select an item in the first list preferably from a data base/excell

  15. 15

    Select item with comma separated data-attribute

  16. 16

    Conditional attribute with data_bag_item in Chef?

  17. 17

    Select item with comma separated data-attribute

  18. 18

    shopping list item data type

  19. 19

    ASP.NET repeater only renders the first item in my XML data source

  20. 20

    Get Data Attribute of First Child Element with jQuery

  21. 21

    HTML list not showing bullet point on FIRST list item ONLY

  22. 22

    Pass first item of subscribed data to service

  23. 23

    Only get part out of data-attribute

  24. 24

    Form just gets data from first row in table

  25. 25

    getting only positive number from a list that containing heterogeneous data type item in python 3

  26. 26

    cloning & deleting list item but prevent deletion of first if it's the item

  27. 27

    RecyclerView set the last ArrayList item data to the first item

  28. 28

    RecyclerView set the last ArrayList item data to the first item

  29. 29

    AJAX send all data but controller gets only part of it

HotTag

Archive