Jquery - how get value ul when click on li?
Asked Answered
B

5

8

Good day.

I have ul list on my page. me need get value ul when i click on li.

Html:

.ddlist {
position: absolute;
width: 316px;
display: none;
background-color: #fff;
border: 1px solid #ccc;
border-top: none;
margin: 0;
padding: 0;
margin-top: -3px;
}

.ddlist li {
list-style: none;
padding: 5px;
}


<ul id="list_t_railway" class="ddlist" style="display: block;">
   <li>Белорусский вокзал</li>
   <li>Казанский вокзал</li>
   <li>Киевский вокзал</li>
</ul>

For get value ul i use script:

$("#list_t_railway").on("click", function(){
      alert($(this).val());

    });

But i get empty value...

Tell me please why i get empty value and how write right?

P.S.: all script for test you can see here

Ballistics answered 23/5, 2013 at 6:48 Comment(3)
what are you trying to get> the .val() looks for the value property of the elementNumerator
val() function is only intended for input/textarea/select elements. what kind of value are you expecting from your ul?Yellowhammer
what do you mean by value ? you want to know how many children of UL ? or value of children ?Headship
D
13

If you want to alert the contents of the li element, you can add a click handler on all li elements.

$("#list_t_railway li").on("click", function(){
  alert($(this).text());

});

Updated fiddle: http://jsfiddle.net/t7ruw/2/

Danadanae answered 23/5, 2013 at 6:50 Comment(0)
C
2

li elements have no value, so .val() won't return anything. Also, there's no event handler bound to the <li> elements themselves:

$("#list_t_railway li").on("click", function(){
  alert($(this).text());
});
Catalogue answered 23/5, 2013 at 6:50 Comment(0)
S
2

try this it will give you same outpuy

$(document).on("click","#list_t_railway li", function(){
  alert($(this).text());
});
Sill answered 23/5, 2013 at 6:53 Comment(0)
S
0

If you want to get ul value then use this.

$("ul#list_t_railway li").on("click", function(){
 alert($("ul#list_t_railway").text());

});
Substandard answered 23/5, 2013 at 7:9 Comment(0)
J
0
$("#list_t_railway li").on("click", function(){alert($(this).html());});
Jarrell answered 18/2, 2015 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.