Tutorial :Why do I get nothing with $('#leftquota').val()?



Question:

<span id="leftquota" style="display:none" value="$row[available]">$row[available]</span>  

JQuery code:

var left=$('#leftquota').val();  alert(left);  


Solution:1

var left=$('#leftquota').attr('value'); alert(left);  

is what you want.

The .attr() method gets(or sets) an attribute of a element. So .attr('value') will get the value of the 'value' attribute.

EDIT: As morning coffee pointed out, the same value of the 'value' attribute is given inside the span.

So we should do

var left=$('#leftquota').text(); alert(left);  

to get the value.

Also, 'value' is not a valid attribute for the span tag.


Solution:2

Because val() takes the value from an input field. value is not a valid attribute of <span>.

You should use .text() since you store the same information as in the value-field inside your <span>.

Example:

var left = $('#leftquota').text();  alert(left);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »