Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery val() is not showing value of span

Tags:

jquery

Here i am trying to get value of span that is 1 here .text() is showing proper value but .val() is not showing anyting

<span class="commentpageno" id="commentpageno_1111" style="visibility: hidden">1</span>  

 $('.commentpageno').click(function (e) {
     alert($('.comment_page_no').text());
     alert($('.comment_page_no').val());
 });
like image 963
manish m Avatar asked Nov 30 '22 21:11

manish m


2 Answers

val() is not used for span or div it is used for input control like text, checkbox etc

The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null, jQuery docs

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method, jQuery docs

like image 103
Adil Avatar answered Dec 09 '22 20:12

Adil


Your class name is commentpageno and you are listening on comment_page_no

Do

alert($('.commentpageno').text());

as .val() would not work for span.

like image 21
karthikr Avatar answered Dec 09 '22 20:12

karthikr