I'm trying to populate my Highcharts graph by a form submit/click function. For some reason I can't get the var to pass any value.
$('#button').click(function() {
var gaA = $('#gaA').val();
var chart = $('#container').highcharts();
chart.series[0].data[0].update(gaA);
chart.series[0].data[1].update(50);
chart.series[0].data[2].update(50);
});
If I change
var gaA = $('#gaA').val();
to
var gaA = 500;
it passes the value of 500. But I need this to populate from the form.
Here is the full code: http://jsfiddle.net/reLuP/1/
val returns a string, but you need a number. Force it to a number with the unary plus operator or parseFloat
chart.series[0].data[0].update(+gaA);
jsFiddle
You should use
var gaA = parseFloat($('#gaA').val());
To receive number format, which is used by highcharts, instead of text value.
Related
In Google Sheets Script, how do I stop limit a number to e.g. 3 decimal points?
For instance, the following code is a section of a function I have created which will send an email of the number in cell 'Q12', however the number I receive is e.g. 1.9468186134852794% instead of 1.94%.
var changeCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheetname").getRange("Q12");
var change = changeCell.getValue();
var changeFixed = change.toFixed(3);
Thanks
function fixingDecimalPlaces() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var v=73.123456789;
sh.getRange('A1').setValue(Number(v).toFixed(3));
}
If you want to send the value in Q12 as it is shown in the sheet (with the same number of decimals), you can use getDisplayValue:
var changeCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheetname").getRange("Q12");
var change = changeCell.getDisplayValue();
This won't change the value in the spreadsheet. If you want to do that, just use setValue:
var changeCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheetname").getRange("Q12");
changeFixed = changeCell.getValue().toFixed(3);
changeCell.setValue(changeFixed);
Reference:
Range.getDisplayValue()
Range.setValue(value)
I have a string in javascript like var Value = "\\\\192.168.0.1\\\\Dir\\NewDir\\Imp\\file.xml",
but the output is (with new lines):
\\\\192.168.0.1
\\\\Dir
\\NewDir
\\Imp
\\file.xml
form.setValues({
'idInput' : Value
});
form - is a form created in extJs.
idInput - is the id of display field
Value - is the new value on a displayfield
How to keep the backslashes without the new lines? I tried with JSON stringify, but it doesn't work.
I'm thinking that - you want to render value with backslases as it is:
var Value = '\\\\192.168.0.1\\\\Dir\\NewDir\\Imp\\file.xml';
Value = encodeURI(Value);
Value = decodeURI(Value.replace(/%5C/g, "%5C%5C"));
Take a look on example : https://fiddle.sencha.com/#view/editor&fiddle/301t
Having some trouble getting this right. I'm very new to jQuery, so trying to get better and learn.
Currently I am getting 2 different values from a html table using the following code
var sellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var buyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
These both output a value such as $13,000,000
I am then wanting to subtract 1 from these values (making it $12,999,999) before pasting them to an input as such
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);
However, I am having some trouble with how to subtract $1 from these.
I tried using sellPrice--; but without success.
I've also tried adding - 1; at the end of each variable, but did not succeed either.
I tried to test something like this, but did not work either.
var minusOne = -1;
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
var sellPrice = (getCurrentSellPrice - minusOne);
var buyPrice = (getCurrentBuyPrice - minusOne);
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);`
Trying my best to familiarize myself with jQuery :)
Any help is much appreciated!
Solved using this
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
var sellPrice = Number(getCurrentSellPrice.replace(/[^0-9\.]+/g,"")) - 1;
var buyPrice = Number(getCurrentBuyPrice.replace(/[^0-9\.]+/g,"")) + 1;
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);
Since your numbers contain currency symbol and are strings, you need to convert them to proper numbers before subtracting them. See the answer below.
How to convert a currency string to a double with jQuery or Javascript?
I am developing a simple application form where I am calculating experiences from maximum three employers. Now I want to add them up . The experiences are in the form of X years Y months and Z days. I have written following javascript function --
function total(){
var td;
var fd=parseInt(document.getElementById("LoS_days1").value);
var sd=parseInt(document.getElementById("LoS_days2").value);
var ld=parseInt(document.getElementById("LoS_days3").value);
var tm;
var fm=parseInt(document.getElementById("LoS_months1").value);
var sm=parseInt(document.getElementById("LoS_months2").value);
var lm=parseInt(document.getElementById("LoS_months3").value);
var ty;
var fy=parseInt(document.getElementById("LoS_year1").value);
var sy=parseInt(document.getElementById("LoS_year2").value);
var ly=parseInt(document.getElementById("LoS_year3").value);
td = (fd +sd +ld);
var rd = td%30;
var cm = Math.floor(td/30);
document.getElementById("Totalexp_day").value=rd;
tm = (cm + fm +sm +lm);
var rm = tm%12;
var cy = Math.floor(ty/12);
document.getElementById("Totalexp_month").value=rm;
ty = (cy + fy +sy +ly);
document.getElementById("Totalexp_year").value=ty;
}
I am getting a NaN message in each of the Totalexp_day, Totalexp_month and Totalexp_day field. Earlier I had some modified code that was not showing NaN message but it was not showing the desired results. Kindly suggest what to do to eliminate these two errors.
parseInt(document.getElementById("LoS_days1").value)
if the first character of the string cannot be converted to a number, parseInt will return NaN.
To avoid this, you can so something like is suggested here:
parseInt(document.getElementById("LoS_days1").value) || 0
If document.getElementById("Totalexp_day").value is empty then also it will return NaN. Make sure you have some number there.
Second alternative is reading document.getElementById("Totalexp_day").innerHTML and then applying parseInt
Probably you alert or console log the document.getElementById("Totalexp_day").value you would be more clearer why this problem is comming
I'm struggling with a ExtJS 4.1.1 grid that has editable cells (CellEditing plugin).
A person should be able to type a mathematic formula into the cell and it should generate the result into the field's value. For example: If a user types (320*10)/4 the return should be 800. Or similar if the user types (320m*10cm)/4 the function should strip the non-mathematical characters from the formula and then calculate it.
I was looking to replace (or match) with a RegExp, but I cannot seem to get it to work. It keeps returning NaN and when I do console.log(e.value); it returns only the originalValue and not the value that I need.
I don't have much code to attach:
onGridValidateEdit : function(editor,e,opts) {
var str = e.value.toString();
console.log(str);
var strCalc = str.match(/0-9+-*\/()/g);
console.log(strCalc);
var numCalc = Number(eval(strCalc));
console.log(numCalc);
return numCalc;
},
Which returns: str=321 strCalc=null numCalc=0 when I type 321*2.
Any help appreciated,
GR.
Update:
Based on input by Paul Schroeder, I created this:
onGridValidateEdit : function(editor,e,opts) {
var str = e.record.get(e.field).toString();
var strCalc = str.replace(/[^0-9+*-/()]/g, "");
var numCalc = Number(eval(strCalc));
console.log(typeof numCalc);
console.log(numCalc);
return numCalc;
},
Which calculates the number, but I am unable to print it back to the grid itself. It shows up as "NaN" even though in console it shows typeof=number and value=800.
Final code:
Here's the final code that worked:
onGridValidateEdit : function(editor,e,opts) {
var fldName = e.field;
var str = e.record.get(fldName).toString();
var strCalc = str.replace(/[^0-9+*-/()]/g, "");
var numCalc = Number(eval(strCalc));
e.record.set(fldName,numCalc);
},
Lets break this code down.
onGridValidateEdit : function(editor,e,opts) {
var str = e.value.toString();
What listener is this code being used in? This is very important for us to know, here's how I set up my listeners in the plugin:
listeners: {
edit: function(editor, e){
var record = e.record;
var str = record.get("your data_index of the value");
}
}
Setting it up this way works for me, So lets move on to:
var strCalc = str.match(/0-9+-*\/()/g);
console.log(strCalc);
at which point strCalc=null, this is also correct. str.match returns null because your regex does not match anything in the string. What I think you want to do instead is this:
var strCalc = str.replace(/[^0-9+*-]/g, "");
console.log(strCalc);
This changes it to replace all characters in the string that aren't your equation operators and numbers. After that I think it should work for whole numbers. I think that you may actually want decimal numbers too, but I can't think of the regex for that off the top of my head (the . needs to be escaped somehow), but it should be simple enough to find in a google search.