tags:

views:

5389

answers:

6

why is this not returning value in li? what am i doing wrong?

$("#list li").click(function() {
        var selected = $(this).val();
        alert(selected);
})
+2  A: 

A li doesn't have a value. Only form-related elements such as input, textarea and select have values.

svinto
+14  A: 

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.

Jason Cohen
+1  A: 

Most probably, you want something like this:

$("#list li").click(function() {
        var selected = $(this).html();
        alert(selected);
});
Andreas Grech
+2  A: 

Use .text() or .html()

$("#list li").click(function() {
        var selected = $(this).text();
        alert(selected);
});
Mark Hurd
+2  A: 
<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

Daxon
A: 

$("#list li").click(function() { var selected = $(this).html(); alert(selected); });

harry