Emmanuel Ikechukwu
Emmanuel Ikechukwu

Reputation: 101

How do I pass a value to list-item

I want want to pass a value to list items but I don't know how.... This is what I want to achieve

<li class="list-group-item" value="" />Option 1</li>

value="" the value should be anything I assign to it

The value should be added to the list with javascript or jquery anyone can go. I just want to assign this value so each list that will be added will have different functions onclick.... How do I do it

Upvotes: 0

Views: 1307

Answers (1)

Jerodev
Jerodev

Reputation: 33186

If you want to add a special attribute to the list item, you should use data-* attributes. These comply with the HTML5 standard.

<li class="list-group-item" data-value="op1" />Option 1</li>

In your jQuery you can now get the value using the data() function.

var value = element.data("value");

If you want to assign a data-* value to an element, just pass a second parameter to the data() function.

element.data("value", "new value");

Upvotes: 5

Related Questions