jquery val() is not showing value of span

jquery

Solution

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

Problem

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()); }); ```

Original source