GetElementById and A tags - target

Getting error on this:
for (i = 1; i <= 18; i++) {
oAllKits = myNode.getElementById('node' + i).getElementsByTagName('a');
}
I have a series of IDs on the html document called: node1, node2,... node18. I am trying to target the A tags on these IDs since these A tags are the only elements inside these ids. The console is giving me this message: # has no method 'getElementById'.
I am doing a for loop because I want the variable oAllKits to hold an all those A tags inside the Ids. Thank you advance for your help.

That can be done easily. Find it here
or you can see the code
var avar = document.getElementById('div');
var bvar = div.getElementsByTagName('a');
var cvar = children.length;
for (var i=0;i < len;i++) {
document.getElementById('aclass').innerHTML +='<br> ' + children[i].href;
}

getElementById exists on the document. There should be only 1 of any particular ID so the selector is fast.
var div = document.getElementById('id1');
var children = div.getElementsByTagName('a');
var len = children.length;
for (var i=0;i < len;i++) {
document.getElementById('found').innerHTML += '<br> ' + children[i].href;
}
http://jsfiddle.net/UhT2W/1/

Related

Get count of characters for translation in Kentico Cloud

Is there a way to tell the count of characters of all text fields in some of our content items? We need to estimate a translation price for our content items.
You can use Delivery API to retrieve your items and run a quick javascript to count the characters for you. First, get all your items (or a subset, depending on what you need) with the call excluding all the modular content (linked items) like this:
https://deliver.kenticocloud.com/<projectid>/items?depth=0​​​​​​​
Then you can use browser console to run this piece of code:
var response = JSON.parse(document.getElementsByTagName("BODY")[0].textContent);
var noOfChars = 0;
for (var x = 0; x < response.items.length; x++) {
var p = response.items[x].elements;
for (var key in p) {
if (p[key].type=='rich_text' || p[key].type=='text') {
noOfChars += strip(p[key].value).length;
}
}
}
noOfChars;
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
And hit enter. This is what the result will look like:

Zapier Javascript Issue

Zapier splits out my JSON arrays to two separate variables when it runs in a code action, any idea why? If I include the array in the code action, it works - but if I pull it from another step it splits it out.
Code:
var jsonOrg = JSON.parse(inputData.data);
var orgList = inputData.origin;
var orgArray = orgList.split(",");
var out =[];
for (var i = 0; i<jsonOrg.rows.length; i++){
for (var j = 0; j<orgArray.length; j++){
if (jsonOrg.rows[i].product === orgArray[j]){
out += jsonOrg.rows.id[i] + ",";
}
}
}
output = [{id: 123, hello:out }];

Get resultSet keys in phonegap?

With say 5 fields in the DB, I know the columns that can be queried and use:
function getDetails_success (tx, results) {
var len = results.rows.length;
for (var i=0; i<len; i++) {
var content = results.rows.item(i);
buf += '<tr '+ content.key1+'>';
buf += '<tr '+ content.key2+'>';
}
}
and so on.
What if I have 50 fields, of which 5 random fields has to be displayed. Do I get the keys from the resultset? What are the various ways I can approach this?
If 5 random fields are selected by the user to be displayed, put all of them in an array.
var randomFieldsSelected = new Array();
randomFieldsSelected.push(selection1);
randomFieldsSelected.push(selection2); //and so on
Instead of the above for loop, put,
for (var i=0; i<len; i++) {
var content = results.rows.item(i);
buf += '<tr '+ content[randomFieldsSelected[i]]+'>';
}
(The above works,provided, the database column names match with 'selection1' and 'selection2' etc)

Hash of a cell text in Google Spreadsheet

How can I compute a MD5 or SHA1 hash of text in a specific cell and set it to another cell in Google Spreadsheet?
Is there a formula like =ComputeMD5(A1) or =ComputeSHA1(A1)?
Or is it possible to write custom formula for this? How?
Open Tools > Script Editor then paste the following code:
function MD5 (input) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
var txtHash = '';
for (i = 0; i < rawHash.length; i++) {
var hashVal = rawHash[i];
if (hashVal < 0) {
hashVal += 256;
}
if (hashVal.toString(16).length == 1) {
txtHash += '0';
}
txtHash += hashVal.toString(16);
}
return txtHash;
}
Save the script after that and then use the MD5() function in your spreadsheet while referencing a cell.
This script is based on Utilities.computeDigest() function.
Thanks to gabhubert for the code.
This is the SHA1 version of that code (very simple change)
function GetSHA1(input) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, input);
var txtHash = '';
for (j = 0; j <rawHash.length; j++) {
var hashVal = rawHash[j];
if (hashVal < 0)
hashVal += 256;
if (hashVal.toString(16).length == 1)
txtHash += "0";
txtHash += hashVal.toString(16);
}
return txtHash;
}
Ok, got it,
Need to create custom function as explained in
http://code.google.com/googleapps/appsscript/articles/custom_function.html
And then use the apis as explained in
http://code.google.com/googleapps/appsscript/service_utilities.html
I need to handtype the complete function name so that I can see the result in the cell.
Following is the sample of the code that gave base 64 encoded hash of the text
function getBase64EncodedMD5(text)
{
return Utilities.base64Encode( Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, text));
}
The difference between this solution and the others is:
It fixes an issue some of the above solution have with offsetting the output of Utilities.computeDigest (it offsets by 128 instead of 256)
It fixes an issue that causes some other solutions to produce the same hash for different inputs by calling JSON.stringify() on input before passing it to Utilities.computeDigest()
function MD5(input) {
var result = "";
var byteArray = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, JSON.stringify(input));
for (i=0; i < byteArray.length; i++) {
result += (byteArray[i] + 128).toString(16) + "-";
}
result = result.substring(result, result.length - 1); // remove trailing dash
return result;
}
to get hashes for a range of cells, add this next to gabhubert's function:
function RangeGetMD5Hash(input) {
if (input.map) { // Test whether input is an array.
return input.map(GetMD5Hash); // Recurse over array if so.
} else {
return GetMD5Hash(input)
}
}
and use it in cell this way:
=RangeGetMD5Hash(A5:X25)
It returns range of same dimensions as source one, values will spread down and right from cell with formulae.
It's universal single-value-function to range-func conversion method (ref), and it's way faster than separate formuleas for each cell; in this form, it also works for single cell, so maybe it's worth to rewrite source function this way.
Based on #gabhubert but using array operations to get the hexadecimal representation
function sha(str){
return Utilities
.computeDigest(Utilities.DigestAlgorithm.SHA_1, str) // string to digested array of integers
.map(function(val) {return val<0? val+256 : val}) // correct the offset
.map(function(val) {return ("00" + val.toString(16)).slice(-2)}) // add padding and enconde
.join(''); // join in a single string
}
Using #gabhubert answer, you could do this, if you want to get the results from a whole row. From the script editor.
function GetMD5Hash(value) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, value);
var txtHash = '';
for (j = 0; j <rawHash.length; j++) {
var hashVal = rawHash[j];
if (hashVal < 0)
hashVal += 256;
if (hashVal.toString(16).length == 1)
txtHash += "0";
txtHash += hashVal.toString(16);
}
return txtHash;
}
function straightToText() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var r = 1;
var n_rows = 9999;
var n_cols = 1;
var column = 1;
var sheet = ss[0].getRange(r, column, n_rows, ncols).getValues(); // get first sheet, a1:a9999
var results = [];
for (var i = 0; i < sheet.length; i++) {
var hashmd5= GetMD5Hash(sheet[i][0]);
results.push(hashmd5);
}
var dest_col = 3;
for (var j = 0; j < results.length; j++) {
var row = j+1;
ss[0].getRange(row, dest_col).setValue(results[j]); // write output to c1:c9999 as text
}
}
And then, from the Run menu, just run the function straightToText() so you can get your result, and elude the too many calls to a function error.
I was looking for an option that would provide a shorter result. What do you think about this? It only returns 4 characters. The unfortunate part is that it uses i's and o's which can be confused for L's and 0's respectively; with the right font and in caps it wouldn't matter much.
function getShortMD5Hash(input) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
var txtHash = '';
for (j = 0; j < 16; j += 8) {
hashVal = (rawHash[j] + rawHash[j+1] + rawHash[j+2] + rawHash[j+3]) ^ (rawHash[j+4] + rawHash[j+5] + rawHash[j+6] + rawHash[j+7])
if (hashVal < 0)
hashVal += 1024;
if (hashVal.toString(36).length == 1)
txtHash += "0";
txtHash += hashVal.toString(36);
}
return txtHash.toUpperCase();
}
I needed to get a hash across a range of cells, so I run it like this:
function RangeSHA256(input)
{
return Array.isArray(input) ?
input.map(row => row.map(cell => SHA256(cell))) :
SHA256(input);
}

Can I iterate through each of my SharedObjects in ActionScript 3?

Is it possible to execute a "foreach" through each of my SharedObjects?
Something like this . . .
_so = SharedObject.getLocal("test","/");
foreach (var item:Object in _so)
{
// print key name and value
}
I'm not sure I understand.
1.Do you want to loop through all your Shared Objects ?
2.Do you want to loop through all your objects inside the data property of a SharedObject ?
1.Assuming sharedObjects is an array containing SharedObject instances
var sharedObjects:Array = ['test','fred','louie'];
var sharedObjectsData:Array = [];
for(var i:int = 0 ; i < sharedObjects.length ; i++){
sharedObjectsData.push(SharedObject.getLocal(sharedObjects[i],"/").data);
}
//or something like that
2.Try using an old school for in loop.
for(var i in _so.data){
trace('property: ' + i + ' value: ' + _so.data[i]);
}
Hope it helps!

Resources