syntax error in GAS script editor - javascript

I have an error in the following code and I can't find why...
Using UiApp I define a couple of ListBox like this in a for loop
var critGlist = app.createListBox().setName('critGlist'+n).setId('critGlist'+n).addChangeHandler(refreshGHandler).addChangeHandler(cHandlerG).setTag(listItem[n]);
I added a TAG to be able to retrieve a value in the handler function because when I add items to this list I do it like this :
for(var c=0;c<listItem[n].length;++c){
critGlist.addItem(listItem[n][c],c);// the returned value is c, the value shown is listItem[n][c]
}
Then in my handler function I retrieve the value c that is the index of an element of the array listItem[n]
Since I stored a stringified value of this array as a tag I have to retrieve the tag first and then using the index I get the desired value...
That's where it becomes problematic !
I tried the 3 following codes :
var idx = Number(e.parameter['critGlist'+c]);// this works and I get the index
var item = e.parameter.critGlist0_tag.split(',')[idx];// this also works for a fixed index (0 here) but I need to use it in a for loop so I tried the code below
var item = e.parameter['critGlist'+c]_tag.split(',')[idx];// this generates an syntax error
// error message :"Signe ; manquant avant l'instruction. (ligne 129, fichier "calculatrice Global")"
// which means : missing ; before statement (line 129...
Am I missing something obvious ? How should I write it differently ?
Obviously it is the underscore that is not accepted... but how could I not use it ?
Well, I have a few other possibilities to get the result I want (using a hidden widget for example or some other temporary storage of even let the listBox return the value instead of the index) but still I'd like to know why this syntax is wrong ...
I'm not asking for a different code (as mentioned before there are a lot of other ways to go) , just some explanation about what is wrong in this code and this #!##å»ÛÁØ underscore ;)

You will need to put the whole property within the brackets as below
var item = e.parameter['critGlist'+c+'_tag'].split(',')[idx];// this generates an syntax error

Related

Updating heading tags dynamically

I have several heading tags with IDs named 'Cat1', 'Cat2', 'Cat3' etc.
<h3 id="Cat1"></h3>
After an Ajax call a function receives an array of category names and this array is passed into another function which I want to use to place each of the categories from the array, into each of the heading tags. This is what I have at present:
for(var x=0; x<category_array.length; x++) {
var word = category_array[x];
var identify = "Cat" + (x+1).toString();
document.getElementById(identify).innerHTML = word;
}
I am currently getting the following error:
Uncaught TypeError: Cannot set property 'innerHTML' of null
Firstly, why is this occuring and secondly, how can I successfully update my heading tags with each of the categories in the array?
EDIT: If I write 'Cat1' into the getElementById script, it will update the h3 tags. But, the variable identify does not seem to be read correctly.
The error is occurring because document.getElementById(identify) returns null, which happens when the element you're trying to find does not exist. Otherwise, your code looks correct.
Make sure you have enough Cat<N> elements on the page to fill. One way to debug this issue is to console.log(identify) then look at the HTML to see if you have all the elements that appear in the console.

Protractor code to extract multiple paragraph text

how to fetch the values of all paragraph (p) at a time ..
for example below is how my inspect view looks like
"testing sample one."
"testing sample two."
and below is my code sample to extract the value of id 'run'
browser.findElement(by.css('[id=run]')).getText()
this just extract the first value or I can modify and get the second value of id.. my need is I need to get both values at one go.. in the same line of code.. could you please advise
Though missing an html-example and a bit more description of what you like to extract, I'll give it a try.
In general you could/should use element.all(by.css()), aka $$(), instead of browser.findElements, except you know exactly, why you use findElements.
Read some details about the difference here.
Then as mentioned in a comment already, there is a findElements() (see API-Doc for findElements() here), returning an array of all values, matching the criteria. Just you can't immediately use getText() on it, as you get an array of elements and getText() requires a single element (see API-Doc for getText() here). Therefore you'd need to pass it through some loop.
Without knowing enough context here a small set of ideas to pick from.
var allP = new Array();
var allPString = null; //if one long string is desired
//here I'm using now element.all() instead of browser.findElements
var allPEl = $$('p#run'); //equal to element.all(by.css('p[id=run]')); //returns array of all found elements
var allPElBrowser = browser.findElements(by.id('run')); //returns array of all found elements
var i = allPEl.length();
var j = 0;
allPEl.each().getText().then(function(text){ //getAttribute('value') instead of getText(), if getText doesn't work.
allP.push(text); //add it to Array
allPString += text+' '; //add to String with a space at the end
j++; //counter
if(i === j-1){continueTest()}; //call continuation at the end of last loop, due to asynchronous nature of 'then()'.
});
continueTest = function(){
allP.toString() //in case of comma separated list from Array is desired
// here comes the rest of your test case logic
};
Note, that I go with the assumption that you need resolved promises, so the content of your <p>'s to continue.
If you can continue just with the array of <p>-objects, which you later resolve within an expect() all you need is $$('p#id');.
If the solution doesn't work for you, let me know, what part is still missing or where problems occur.

addition in javascript is resulting in an object

I am tring to add 2 values together in a javascript application however when I try and do this the output I get is;
Total Price £[object Object]116.96
Where [object Object] is the value I am trying to add to 116.96
the code I am using to do the addition is bellow;
document.getElementById("getTotal").addEventListener("click", function()
{
var STotal = (($('#SeatPrice')+(subTotal)).toString());
$('#total').text(STotal);
});
these are where the values for '#seatPrice' and '#total' are derived from
$('#total').text(subTotal.toString());
$('#SeatPrice').text((($('td.selected').length)+count)*pricing);
If anyone has any ideas on how to resolve this issue please let me know.
Thanks!
The problem lies here: var STotal = (($('#SeatPrice')+(subTotal)).toString());
$('#SeatPrice') is a jquery function which returns an object - the html element you've searched for. So when you add that to your subTotal, you are adding the object to a string which, in javascript, will just create a string out of both.
You probably want to get the value of that element using something like $('#SeatPrice').val() or $('#SeatPrice').text()

Java Script + JSON Parsing and converting into Array

I'm looking for help in converting a particular elements in JSON message to an array using java script at run time. We wanted the script to be more generic. Actually we were trying the following which worked for single element and while changing it to handle for multiple elements at run time its not working.
//Working for Single element - Static
var bodyContext = JSON.parse(response.content)
if(bodyContext.companylist.company.constructor !== Array){
bodyContext.companylist.company = [bodyContext.companylist.company]
}
The above code works and converts Company in JSON message as a Array, Where as the below we tried for multiple elements is not working
//Not Working for multiple elements - dynamic
var bodyContext = JSON.parse(response.content)
var elementName = "";
//Loop runs every time and changes the value of elementName at every iteration
if(bodyContext.elementName .constructor !== Array){ //not working
bodyContext.elementName = [bodyContext.elementName] //Not working
}
instead of looking for "bodyContext.companylist.company" and converting into Array, "bodyContext.elementName" is checked and added to the bodycontext object.
how to handle this. ElementName variable along with JavaScript object is not recognized.
Please help.
you can JSON.parse(data) then you can fetch data from Javascript object like
$.each(Obj,function(key,value){
});
You'll want to use
bodyContext[elementName]
since
bodyContext.elementName
looks for a field in bodyContext named elementName, not the a field named after the value in elementName.
Also, you initialize elementName with "", and this won't match anything on the first iteration.

Google Chrome not letting me use setAttribute for ID's

I've got some JS code here. Basically, I am trying to change the ID of an element to some value from a previous variable.
Here's what I got so far;
function() {
var colorarray = [ "RANDOMCOLOR_0", "RANDOMCOLOR_1", "RANDOMCOLOR_2" ];
var RANcolorarray = colorarray[Math.rsound(Math.random() * (colorarray.length - 1))];
document.getElementsByClassName('RANDOMCOLOR').setAttribute('id', RANcolorarray);
}
This code throws an error in Chrome for line 4: Uncaught TypeError: undefined is not a function which is weird because JsLint finds no errors.
I also tried using the other way to setting id's;
document.getElementsByClassName('RANDOMCOLOR').id = RANcolorarray;
However, although this method does not throw an error on chrome or jslint - it does not work at all after inspecting the element.. :/
Any ideas?
document.getElementsByClassName('RANDOMCOLOR') returns a list of DOM nodes (even if there's only one match) so you can't just call .setAttribute() on the list as the list doesn't have that method.
You can either get the first item out of the list and call .setAttribute() on that one or use a for loop to iterate through the list and call it on all of them. Of course, since you're setting the id, you should not be setting multiple elements to the same id, so I'll assume you just want one element:
document.getElementsByClassName('RANDOMCOLOR')[0].id = RANcolorarray;
Or, a little more safe:
var elems = document.getElementsByClassName('RANDOMCOLOR');
if (elems && elems.length) {
elems[0].id = RANcolorarray;
}

Categories

Resources