I have a variable named colWidth in jquery which runs some math on numbers grabbed from an input using jquery. How do I set a value of an input field with an id of #test to the value of colWidth?
$("input#width").keyup(function () {
var value = $(this).val();
$("span#width-text").text(value);
}).keyup();
$("input#columns").keyup(function () {
var value = $(this).val();
$("span#columns-text").text(value);
}).keyup();
$("input#gutter").keyup(function () {
var value = $(this).val();
$("span#gutter-text").text(value);
}).keyup();
$("input#gutter").keyup(function () {
var value = $(this).val();
$("span#gutter-text").text(value);
}).keyup();
$(function(){
$('a#calc').click(function(){
var width = $('input#width').val();
var col = $('input#columns').val();
var gutter = $('input#gutter').val();
var newWidth = width / col;
var colSize = (gutter + gutter) * col;
var colWidth = newWidth - colSize;
$('input#test').val(colWidth);
});
});
undefinedor are not a number, you'll get a result ofNaNwhich means Not a Number. You shouldconsole.log()oralert()each of your variables to see if it is the value you expect. Also, be aware that.val()returns a String. Some math operators will accept a String, and convert it for you, but the+operator will concatenate the string. So ifgutteris10, thengutter + gutterwill be1010.