Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(this).val() not working to get text from span using jquery

Tags:

html

jquery

Giving this html, i want to grab "August" from it when i click on it:

<span class="ui-datepicker-month">August</span> 

i tried

$(".ui-datepicker-month").live("click", function () {     var monthname =  $(this).val();     alert(monthname); }); 

but doesn't seem to be working

like image 562
leora Avatar asked Aug 25 '10 15:08

leora


People also ask

How do I get Span Innertext?

Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants.

Does span have value attribute?

Default value of HTML span attribute is 1.


2 Answers

Instead of .val() use .text(), like this:

$(".ui-datepicker-month").live("click", function () {     var monthname =  $(this).text();     alert(monthname); }); 

Or in jQuery 1.7+ use on() as live is deprecated:

$(document).on('click', '.ui-datepicker-month', function () {     var monthname =  $(this).text();     alert(monthname); }); 

.val() is for input type elements (including textareas and dropdowns), since you're dealing with an element with text content, use .text() here.

like image 73
Nick Craver Avatar answered Oct 12 '22 06:10

Nick Craver


I think you want .text():

var monthname = $(this).text(); 
like image 29
Matthew Jones Avatar answered Oct 12 '22 06:10

Matthew Jones