Why is it not returning value in 'li'? What am i doing wrong?
$("#list li").click(function() { var selected = $(this).val(); alert(selected); })
To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.
jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.
val() method is primarily used to get the values of form elements such as input , select and textarea . When called on an empty collection, it returns undefined .
jQuery get() Method get() method loads data from the server using a HTTP GET request.
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.
<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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With