I'm trying to add through a jquery event and I'm getting NaN. What am I missing?
<input type="hidden" id="skillcount" name="skillcount" value="3" onchange="valueadd(this)"/>
function valueadd(ok){
var value=parseFloat($(this).val())+1;
}
The code should be:
function valueadd(ok){
// "this" inside here refers to the window
var value=parseFloat(ok.value)+1;
}
The inline onchange is actually an anonymous function:
function() {
//"this" inside here refers to the element
valueadd(this);
}
So "this" is an argument that gets called "ok" in the valueadd scope. As the others have stated, though, you probably want to use jquery's bind so "this" inside of valueadd will point to the element.
this is a reserved word in JavaScript, so you can't use it in the function argument signature.
I'd probably change that code to...
$('#skillcount').change(function() {
var value = parseFloat($(this).val()) + 1;
});
...and drop the inline event handler.
To check if parseFloat() returns NaN, use isNaN().
this is the element.this cannot be used as a param name or anything other than to access the object in scope.Use:
<input type="hidden" id="skillcount" name="skillcount" value="3" />
<script type="text/javascript">
$( '#skillcount' ).bind( 'change', function()
{
var value = parseFloat( $( this ).val() ) + 1;
} );
</script>
focus, change of value, and blur. Hidden inputs can never gain or lose focus, so they never "change"