Why is it not returning value in 'li'? What am i doing wrong?
$("#list li").click(function() {
var selected = $(this).val();
alert(selected);
})
This question is related to
jquery
You can do the same by using jQuery on().
$("#list").on('click','li',(function() {
var selected = $(this).text(); //or .html()
alert(selected);
})
To get value of li we should use $("#list li").click(function() { var selected = $(this).html();
or
var selected = $(this).text();
alert(selected);
})
$("#list li").click(function() {
var selected = $(this).html();
alert(selected);
});
Most probably, you want something like this:
$("#list li").click(function() {
var selected = $(this).html();
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
A li doesn't have a value. Only form-related elements such as input, textarea and select have values.
Use .text() or .html()
$("#list li").click(function() {
var selected = $(this).text();
alert(selected);
});
<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());
});
});
Source: Stackoverflow.com