Get value attribute from an li element

Gaurav Sachdeva

I have an unordered list with li elements in it. When I click on that particular item in the list, I want to extract the value of that li item and use it. In the example below I would want to extract the value "9".

How can I do this in jQuery?

<li sequence="1" title="Category 1" class="liEllipsis selSubCategories" value="9">
    <a href="#">
        <span class="viewIcons delFaceName _delete fl"></span>
        Category 1
    </a>
</li>
Rory McCrossan

Firstly, there is no value attribute available on an li element, and adding non-standard attributes will mean that your page is invalid. Instead, use data-* attributes.

You can then hook to the click event of the a element and get that data attribute from its parent li, like this:

$('li a').click(function(e) {
  e.preventDefault();
  var value = $(this).closest('li').data('value'); // = 9
  console.log(value);

  // do something with the value here...
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li data-sequence="1" title="Category 1" class="liEllipsis selSubCategories" data-value="9">
    <a href="#">
      <span class="viewIcons delFaceName _delete fl"></span> Category 1
    </a>
  </li>
  <li data-sequence="1" title="Category 2" class="liEllipsis selSubCategories" data-value="10">
    <a href="#">
      <span class="viewIcons delFaceName _delete fl"></span> Category 2
    </a>
  </li>
</ul>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get value from inside <li> element and put it to image attribute tag using jQuery

From Dev

AWK get attribute value from XML element

From Dev

how to get the value from <h> element in <li> element

From Dev

JQuery - Get the ID attribute of li element

From Dev

Get attribute from HTML element that is a json value using JS

From Dev

Angularjs | how to get an attribute value from element in which controller is defined

From Dev

How get parent element attribute and child value from XML in java?

From Dev

get element attribute value in Protractor

From Dev

get attribute value from multiple elements with same name and attribute value of one other element in xml

From Dev

Get the value from a present in the li

From Dev

JQuery get the value from <li>

From Dev

How get all value on li element

From Dev

jQuery get element attribute from nested element

From Dev

Get attribute value in SimpleXML object by element and another attribute value

From Dev

Get a value from @href attribute

From Dev

Get property from attribute value

From Dev

get value from <input> which is in a <li> and append value into <li> as a link (a)

From Dev

How to get the attribute value of the last element?

From Dev

Angular element directive how to get attribute value

From Dev

CSS Selector to get the element attribute value

From Dev

DOM structure, get element by attribute name/value

From Dev

Trying to get the href attribute value of parent element

From Dev

Jquery: Get attribute value of dynamically created element

From Dev

Trying to get the href attribute value of parent element

From Dev

android SAXparser get element use attribute value

From Dev

Get xml element with specific attribute value in XmlDocument

From Dev

Get element depending on value of attribute within

From Dev

How to get the value of the attribute of element in angular 2?

From Dev

Deserialize XML element from InnerText and Value attribute

Related Related

  1. 1

    How to get value from inside <li> element and put it to image attribute tag using jQuery

  2. 2

    AWK get attribute value from XML element

  3. 3

    how to get the value from <h> element in <li> element

  4. 4

    JQuery - Get the ID attribute of li element

  5. 5

    Get attribute from HTML element that is a json value using JS

  6. 6

    Angularjs | how to get an attribute value from element in which controller is defined

  7. 7

    How get parent element attribute and child value from XML in java?

  8. 8

    get element attribute value in Protractor

  9. 9

    get attribute value from multiple elements with same name and attribute value of one other element in xml

  10. 10

    Get the value from a present in the li

  11. 11

    JQuery get the value from <li>

  12. 12

    How get all value on li element

  13. 13

    jQuery get element attribute from nested element

  14. 14

    Get attribute value in SimpleXML object by element and another attribute value

  15. 15

    Get a value from @href attribute

  16. 16

    Get property from attribute value

  17. 17

    get value from <input> which is in a <li> and append value into <li> as a link (a)

  18. 18

    How to get the attribute value of the last element?

  19. 19

    Angular element directive how to get attribute value

  20. 20

    CSS Selector to get the element attribute value

  21. 21

    DOM structure, get element by attribute name/value

  22. 22

    Trying to get the href attribute value of parent element

  23. 23

    Jquery: Get attribute value of dynamically created element

  24. 24

    Trying to get the href attribute value of parent element

  25. 25

    android SAXparser get element use attribute value

  26. 26

    Get xml element with specific attribute value in XmlDocument

  27. 27

    Get element depending on value of attribute within

  28. 28

    How to get the value of the attribute of element in angular 2?

  29. 29

    Deserialize XML element from InnerText and Value attribute

HotTag

Archive