Reputation: 13756
Why is it not returning value in 'li'? What am i doing wrong?
$("#list li").click(function() {
var selected = $(this).val();
alert(selected);
})
Upvotes: 22
Views: 116366
Reputation: 165
To get value of li we should use $("#list li").click(function() { var selected = $(this).html();
or
var selected = $(this).text();
alert(selected);
})
Upvotes: 0
Reputation: 41832
You can do the same by using jQuery on().
$("#list").on('click','li',(function() {
var selected = $(this).text(); //or .html()
alert(selected);
})
Upvotes: 1
Reputation: 11
<div class="inter">
<p>Liste des Produits</p>
<ul>
<li><a href="#1">P1</a></li>
<li><a href="#2">P2</a></li>
<li><a href="#3">P3</a></li>
</ul>
</div>
$(document).ready(function(){
$(".inter li").bind(
"click", function(){
alert($(this).children("a").text());
});
});
Upvotes: 1
Reputation: 1
$("#list li").click(function() {
var selected = $(this).html();
alert(selected);
});
Upvotes: 0
Reputation: 1407
<ul id="unOrderedList">
<li value="2">Whatever</li>
.
.
$('#unOrderedList li').click(function(){
var value = $(this).attr('value');
alert(value);
});
Your looking for the attribute "value" inside the "li" tag
Upvotes: 7
Reputation: 83011
Did you want the HTML or text that is inside the li
tag?
If so, use either:
$(this).html()
or:
$(this).text()
The val()
is for form fields only.
Upvotes: 59
Reputation: 13076
Use .text() or .html()
$("#list li").click(function() {
var selected = $(this).text();
alert(selected);
});
Upvotes: 3
Reputation: 107940
Most probably, you want something like this:
$("#list li").click(function() {
var selected = $(this).html();
alert(selected);
});
Upvotes: 2
Reputation: 12488
A li doesn't have a value. Only form-related elements such as input, textarea and select have values.
Upvotes: 2