user3799358
user3799358

Reputation: 15

Getting a value from li

I have a list like this

<ul class="dropdown-menu btn-block pull-left" role="menu" aria-labelledby="dropdownMenu1">

    <li value="http://www.example.com/" role="presentation">
        <a role="menuitem" tabindex="-1" href="#">Example</a>
    </li>

</ul>

and Im trying to get the value "http://wwww.example.com" from the li item using this jquery code

$(".dropdown-menu li").click(function(){
    console.log($(this).val());
});

but the jquery is selecting the a element because the $(this).val() is showing up as 0 and the $(this).text() is 'Example'. How do I get the value of the li element? I tried jquerys .parent() but that didn't work

Upvotes: 0

Views: 58

Answers (2)

tymeJV
tymeJV

Reputation: 104775

value isn't a valid attribute on li - you should be using custom data-* attributes to pass validation:

<li data-value="http://www.example.com/" role="presentation">

And then do:

var site = $(this).data("value");

Upvotes: 3

dave
dave

Reputation: 64657

You have to grab it as an attribute because it is not a form element:

console.log($(this).attr('value'));

Upvotes: 2

Related Questions