Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get element value in jQuery

Tags:

Why is it not returning value in 'li'? What am i doing wrong?

$("#list li").click(function() {     var selected = $(this).val();     alert(selected); }) 
like image 739
zsharp Avatar asked Feb 15 '09 20:02

zsharp


People also ask

How can get input tag value in jQuery?

To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.

How do I set the value of a element in jQuery?

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.

What does VAL () do in jQuery?

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 .

What is get () in jQuery?

jQuery get() Method get() method loads data from the server using a HTTP GET request.


2 Answers

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.

like image 153
Jason Cohen Avatar answered Oct 26 '22 00:10

Jason Cohen


<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

like image 28
Daxon Avatar answered Oct 26 '22 00:10

Daxon