Jitender
Jitender

Reputation: 7971

String value in li's value attribute using jquery

I have string value in li's value attributes but it is giving me 0 when I am trying to get with jquery. Any Idea why? fiddle

$(function(){
alert($('li').attr('value'))    
})

<li value="CAD"></li>

Upvotes: 1

Views: 351

Answers (5)

Ramniwas chhimpa
Ramniwas chhimpa

Reputation: 205

try this one fiddle

<li data-value="CAD"></li>




alert($('li').data('value'))

Upvotes: 0

Felipe Oriani
Felipe Oriani

Reputation: 38598

For custom properties you could use $.data() method, for sample:

alert($('li').data('value'));  

and in your html use the prefix data-,

<li data-value="CAD"></li>

You also could use $.data() method to set values in a data custom property, for sample:

$('li').data('value', '123');

Upvotes: 2

Sri Tirupathi Raju
Sri Tirupathi Raju

Reputation: 819

In html value is a predefined attribute for li which can store only number. you can find it here. As it can be used to store number of the list.

http://www.w3schools.com/tags/tag_li.asp

Upvotes: 1

bhushya
bhushya

Reputation: 1317

You can try following..

Html:

<li data-value="CAD"></li>

JS:

alert($('li').data('value')); 

Here is demo : http://jsfiddle.net/ed8ZL/

Upvotes: 1

m1k1o
m1k1o

Reputation: 2364

You can use data- prefix.

http://jsfiddle.net/37pAZ/1/

$(function(){
alert($('li').attr('data-value'))    
})

<li data-value="CAD"></li>

Upvotes: 1

Related Questions