I have the following HTML:
<tr id="row_1" data-rk="02000008" data-pk="0001I">
When I check this with firebug it shows as:
dataset DOMStringMap { rk="02000008", pk="0001I"}
pk "0001I"
rk "02000008"
I then use jQuery to read this:
var pk = $("tr[id='row_" + row + "']").data('pk')
var rk = $("tr[id='row_" + row + "']").data('rk')
However this is what the firebug debuggers shows when I check the values in my javascript. They are also the same values that are later sent to my C# code in an Ajax call.
pk shows as "0001I"
rk shows as 2000008
Can someone explain this? Both rk and pk are coded in the same way, both have one or more leading zeros but the one returns as a string and the other as a number.
Javascript autoparses so the trailing 'I' in "0001I" causes javascript to interpret it as a string
This is by design:
Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.
What you have to use instead is:
var pk = $("tr[id='row_" + row + "']").attr('data-pk')
var rk = $("tr[id='row_" + row + "']").attr('data-rk')
Try:
var pk = $("tr[id='row_" + row + "']").attr('data-pk');
Since jQuery is messing up your data, you can do:
var rk = document.getElementById('row_' + row]).getAttribute('data-rk');
and you are guaranteed a string.
Related
I'm trying to call this API response using a custom function where I can change the parameters. This is my code:
function callCandles(pair, start, end) {
var response = UrlFetchApp.fetch("https://api-pub.bitfinex.com/v2/candles/trade:1D:" + "pair" + "/hist?limit=1000&start=" + "start" +"&end=" +"end" +"&sort=-1");
var fact = JSON.parse(response.getContentText()); //parse the data from the API and store it in the variable data and convert response to text format//
return fact;
}
This is what I'm typing in the spreadsheet:
=callCandles(tBTCUSD,"1577841154000","1606785154000")
But when I do it I get a "reference does not exist" error.
Thank you.
Please modify as follows and test it again.
Modified script:
function callCandles(pair, start, end) {
var response = UrlFetchApp.fetch("https://api-pub.bitfinex.com/v2/candles/trade:1D:" + pair + "/hist?limit=1000&start=" + start + "&end=" + end + "&sort=-1"); // Modified
var fact = JSON.parse(response.getContentText()); //parse the data from the API and store it in the variable data and convert response to text format//
return fact;
}
Please use pair, start and end as the variables.
I thought that the reason of your error message is due to this. And also, please modify as follows.
Modified formula:
=callCandles("tBTCUSD","1577841154000","1606785154000")
Please use "tBTCUSD" as a string enclosed by ".
Note:
For example, when pair is used as "pair", pair is the string value. By this, even when =callCandles("tBTCUSD","1577841154000","1606785154000") is used, "tBTCUSD" is not used and "pair" is used.
When =callCandles(tBTCUSD,"1577841154000","1606785154000") is used, in this case, tBTCUSD is used as the named range. By this, when there is no named range of tBTCUSD, #NAME? is returned. By this, pair of function callCandles(pair, start, end) { is #NAME?. Please be careful this.
Can you be more specific, in which line does the error occur?
Maybe the problem is that the variable you are passing the function are actually not used:
var response = UrlFetchApp.fetch("https://api-pub.bitfinex.com/v2/candles/trade:1D:" + "pair" + "/hist?limit=1000&start=" + "start" +"&end=" +"end" +"&sort=-1");
The parameter pair, start and end should be added without quotation marks. You should also use the newer syntax for some cleaner code:
var response = UrlFetchApp.fetch(`https://api-pub.bitfinex.com/v2/candles/trade:1D:${pair}/hist?limit=1000&start=${start}&end=${end} &sort=-1`);
And as #tanaike pointed out, use tBTCUSD with quotation mark like this "tBTCUSD"
I have javascript variable var result as following which has java variable that have JSON data like this
var result = <%=JsonData1%> ;
alert(result.toSource());
Above code similar to this code as showing on alert message
var result= [{"year":"12","value":"6694"},{"year":"13","value":"50"},{"year":"08","value":"4776"},{"year":"09","value":"29006"},{"year":"10","value":"1751"}];
but I need to place a single quote on JSON data
'[{"year":"12","value":"6694"},{"year":"13","value":"50"},{"year":"08","value":"4776"},{"year":"09","value":"29006"},{"year":"10","value":"1751"}]'
and place it in new javascript variable like this
var json_pre = result;
alert(json_pre);
but when I change my result data that placed in new json_pre variable as in this link How to add single quote in the variable in Javascript?
var json_pre = "'" +result+ "'";
alert(json_pre.toSource());
then json data shows me like this on the alert message
'[{year:(new Date(-2208058200000)), value:6694}, {year:(new Date(-2207971800000)), value:50}, {year:(new Date(-2208403800000)), value:4776}, {year:(new Date(-2208317400000)), value:29006}, {year:(new Date(-2208231000000)), value:1751}]'
As I have tried every method that given me on that link.So anyone can help me in this?
result is not JSON, but a JavaScript object.
What you need to do is to stringify your object to JSON and then add the quotes:
var result = [{"year":"12","value":"6694"},{"year":"13","value":"50"},{"year":"08","value":"4776"},{"year":"09","value":"29006"},{"year":"10","value":"1751"}];
var result_pre = "'" + JSON.stringify(result) + "'";
console.log(result_pre);
But it is questionable why you would even need that.
You need to understand the single quotes does not make an object a string. When you are trying to store it in a variable I guess you are making the same mistake again.
when you do this
a={"key":"value"}
b="'" + a + "'";
You get a string representation for consoles, if you want a JSON string to be stored in a variable you need to do this
var json = JSON.stringify(result);
alert(json);
You don't need quotes for that.
I have a string that is JSON values separated by /r. It's sort of like records in a DB table. It looks like:
"{"id":"id","hole":"hole","stat":"stat","value":"value"}/r{"id":1354075540949,"hole":"1","stat":"score","value":"4"}/r{"id":1354075540949,"hole":"1","stat":"putts","value":"1"}/r{"id":1354075540949,"hole":"1","stat":"fir","value":"y"}/r{"id":1354075540949,"hole":"1","stat":"gir","value":"n"}/r"
The first row is the column names (id, hole, stat, value) and I just give them the same value. All other rows separated by /r is the actual data.
I split this string by /r, then loop through the result and push the result of JSON.parse() of each element to an array so now I have an array of objects with properties of the given structure (id, hole, stat, value). Everything is working except the 'id' field ends up being true or false instead of the big long number. Why is it doing that?
var tblData = localStorage.getItem(tblName).split("/r");
var data = new Array();
// fill the array
for (i = 1; i < tblData.length - 1; i++)
data.push(JSON.parse(tblData[i]));
[EDIT]
Seems this does work, but there is a jQuery.grep() I run right after this that's setting the id properties to true/false.
var changeRecords = jQuery.grep(data, func);
Where func is:
function (v) { return v.id == gCurrentRoundID && v.hole == gCurrentHole; }
Not sure why it would be setting id to true/false though.
[EDIT2]
Nevermind, I found my error. The function above wasn't the right one and the one I did have only had 1 equal sign for v.id = gCurrentRoundID, which is why it was setting it to true/false.
I would just manually change the whole string to valid JSON. Have it start with a [ and end with a ], then replace all those /rs with commas. The end result should look like
"[{"id":"id","hole":"hole","stat":"stat","value":"value"},{"id":1354075540949,"hole":"1","stat":"score","value":"4"},{"id":1354075540949,"hole":"1","stat":"putts","value":"1"},{"id":1354075540949,"hole":"1","stat":"fir","value":"y"},{"id":1354075540949,"hole":"1","stat":"gir","value":"n"},]"
Then parse that through JSON.parse
Just note that that last trailing comma may cause problems in IE8. If so, you should be able to manually fix that fairly easily. Something like s = s.substr(0, s.length - 2) + ']';
I'm trying to insert a variable's value into a url, but it's not working; I'm just getting the variable not the value
'myid' and 'verif' are the variables and their values are integers.
This code inserts the url into a hidden field in a form
$('#return').val(http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=myid&packid=1&verif=verif&jcode=xxx111xxx);
How do I write the following url so the variables 'myid' and 'verif' are converted to their values?
Well you are missing quotes so your code would not work at all.
$('#return').val("http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=" + myid + "&packid=1&verif=" + verif + "&jcode=xxx111xxx");
You should probably use encodeURIComponent()
You need to quotes " " the strings and concat the variables +
Try
$('#return').val("http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid="+myid+"&packid=1&verif="+verif+"&jcode=xxx111xxx");
JavaScript does not support string interpolation. Try something like this.
myIdVal = encodeURIComponent(myId);
verifVal = encodeURIComponent(verif);
var url = "http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=" + myidVal + "&packid=1&verif=" + verifVal + "&jcode=xxx111xxx";
$('#return').val(url);
A simple string works for me:
given index = 2,
`a.setAttribute("href", "myDirectory/" + index + ".jpg");` links the anchor to
"myDirectory/2.jpg", ie. the file number is taken from variable index.
Not sure if the setAttribute tolerates multiple tokens in its second parameter, but generally, this works.
my problem is with appending a array into existing div as a text. I cant figure it out why its not working, so i have this code:
var raya = ui.item.value + ' ';
$('#result').append(raya);
var tor = $("#result").text();
Above code is working, the value of raya (which is string) is appended correctly into #result
Problem comes here, the value of array1 is not appended to #result2 and ideas why its not working?
var array1 = new Array();
array1 = tor.split( " " );
array1 = $.unique(array1);
$('#result2').append(array1);
return false;
(just to mention that everything is holded in 1 function, this cant be the reason, but just to know)
That's because append expects a string and you're sending it an array.
Two ways to solve it. Use either .toString() or .join()
$('#result2').append(array1.toString()); //inserts a comma separated list
$('#result2').append(array1.join(' ')); //inserts a string separated by what you pass as param
you can do something like this to explicitly convert array to string.
$('#result2').append(array1+'');
Here's a working example
http://jsfiddle.net/EsnLs/2/