Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery attr returns 0 instead of value

Tags:

jquery

So i have a very simple html:

​<ul>
    <li value="test">test</li>
</ul>​​​​​​​​​​​​​​​​​​​​​​​

And this jquery script

alert($("li").attr("value"));

It should return test but it returns 0, why is this happening?

like image 767
Linas Avatar asked Jan 16 '23 01:01

Linas


1 Answers

If you want to return the text inside the li tag then:

alert($("li").text());​

You can't use the 'value' attribute(The 'value' attribute in the li tag can only be numeric). Rename it to data-value:

alert($("li").attr("data-value"));​
like image 52
Kirill Ivlev Avatar answered Jan 28 '23 11:01

Kirill Ivlev