why is this not returning value in li? what am i doing wrong?
$("#list li").click(function() {
var selected = $(this).val();
alert(selected);
})
why is this not returning value in li? what am i doing wrong?
$("#list li").click(function() {
var selected = $(this).val();
alert(selected);
})
A li doesn't have a value. Only form-related elements such as input, textarea and select have values.
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.
Most probably, you want something like this:
$("#list li").click(function() {
var selected = $(this).html();
alert(selected);
});
Use .text() or .html()
$("#list li").click(function() {
var selected = $(this).text();
alert(selected);
});
<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
$("#list li").click(function() { var selected = $(this).html(); alert(selected); });