Javascript refactoring similar variables into a forloop - javascript

I am trying to simplify code where I define a lot of variables which have a similar structure. i.e:
To be simplified:
var monthActual = document.createElement('input');
monthActual.name = "monthActual_" + showrooms[i];
monthActual.value = monthActualData;
fullForm.appendChild(monthActual);
var monthTarget = document.createElement('input');
monthTarget.name = "monthTarget_" + showrooms[i];
monthTarget.value = monthTargetData;
fullForm.appendChild(monthTarget);
var priorYear = document.createElement('input');
priorYear.name = "priorYear_" + showrooms[i];
priorYear.value = priorYearData;
fullForm.appendChild(priorYear);
var priorYearToDate = document.createElement('input');
priorYearToDate.name = "priorYearToDate_" + showrooms[i];
priorYearToDate.value = priorYearToDateData;
fullForm.appendChild(priorYearToDate);
var yearToDateTarget = document.createElement('input');
yearToDateTarget.name = "yearToDateTarget_" + showrooms[i];
yearToDateTarget.value = yearToDateTargetData;
fullForm.appendChild(yearToDateTarget);
var yearToDateActual = document.createElement('input');
yearToDateActual.name = "yearToDateActual_" + showrooms[i];
yearToDateActual.value = yearToDateActualData;
fullForm.appendChild(yearToDateActual);
var YTDVsPYTDSalesCurrency = document.createElement('input');
YTDVsPYTDSalesCurrency.name = "YTDVsPYTDSalesCurrency_" + showrooms[i];
YTDVsPYTDSalesCurrency.value = YTDVsPYTDSalesCurrencyData;
fullForm.appendChild(YTDVsPYTDSalesCurrency);
var YTDVsPYTDSalesinPercent = document.createElement('input');
YTDVsPYTDSalesinPercent.name = "YTDVsPYTDSalesinPercent_" + showrooms[i];
YTDVsPYTDSalesinPercent.value = YTDVsPYTDSalesinPercentData;
fullForm.appendChild(YTDVsPYTDSalesinPercent);
var YTDVsYTDTargetinSalesCurrency = document.createElement('input');
YTDVsYTDTargetinSalesCurrency.name = "YTDVsYTDTargetinSalesCurrency_" + showrooms[i];
YTDVsYTDTargetinSalesCurrency.value = YTDVsYTDTargetinSalesCurrencyData;
fullForm.appendChild(YTDVsYTDTargetinSalesCurrency);
var YTDVsYTDTargetinPercent = document.createElement('input');
YTDVsYTDTargetinPercent.name = "YTDVsYTDTargetinPercent_" + showrooms[i];
YTDVsYTDTargetinPercent.value = YTDVsYTDTargetinPercentData;
fullForm.appendChild(YTDVsYTDTargetinPercent);
I've tried to simplify it by putting the variables into an array then iterating through them like so:
Attempted simplify:
var tableColumnData = ['monthActual', 'monthTarget', 'priorYear', 'priorYearToDate', 'yearToDateTarget',
'yearToDateActual', 'YTDVsPYTDSalesCurrency', 'YTDVsPYTDSalesinPercent', 'YTDVsYTDTargetinSalesCurrency', 'YTDVsYTDTargetinPercent'];
for(var j=0; j<tableColumnData.length; j++){
var temp = document.createElement('input');
temp.name = tableColumnData[j]+ "_" + showrooms[i];
temp.value = (tableColumnData[j] +"Data");
fullForm.appendChild(temp);
}
This is however only giving me the string value of the literal string: tableColumnData[j] +"Data" when I am trying to point towards the variable of the same name. I am not sure how to use the variable of the same name.
Full code:
var fullForm = document.createElement('form');
fullForm.action = '/fpdf/requests/print_fullreport.php?year=' + requestYear + '&month=' + getMonth(requestMonth);
fullForm.id = 'fullForm';
fullForm.target = '_blank';
fullForm.method = 'post';
var showrooms = [1, 3, 4, 24, 27, 29, 34, 36, 37, 8, 21, 25, 26, 28, 31, 33, -1];
for (var i = 0; i <showrooms.length; i++){
var showroomData = allTargetsData.monthlyDetail[showrooms[i]];
var currencyData = showroomData.currency;
var showroomname = showroomData.showroom_name;
var monthActualData = showroomData.total;
var monthTargetData = Math.round(allTargetsData.originalTarget[requestYear][showrooms[i]][requestMonth]['amount']);
var priorYearData = allTargetsData.realFigure[requestYear - 1][showrooms[i]]['figures'][requestMonth];
var priorYearToDateData = showroomData.ly_ytd;
var yearToDateTargetData = Math.round(showroomData.ytd_target);
var yearToDateActualData = showroomData.ytd;
var calculation1 = (showroomData.ytd - showroomData.ly_ytd).toFixed(2);
var YTDVsPYTDSalesCurrencyData = calculation1;
var calculation2 = (parseFloat(showroomData.ytd - showroomData.ly_ytd)/showroomData.ly_ytd).toFixed(2);
var YTDVsPYTDSalesinPercentData = (calculation2*100);
if (isNaN(YTDVsPYTDSalesinPercentData) || YTDVsPYTDSalesinPercentData == "Infinity"){
YTDVsPYTDSalesinPercentData = 0;
}
var calculation3 = (showroomData.ytd - showroomData.ytd_target).toFixed(2);
var YTDVsYTDTargetinSalesCurrencyData = (calculation3*100)/100;
var calculation4 = (parseFloat(showroomData.ytd - showroomData.ytd_target)/parseFloat(showroomData.ytd_target)).toFixed(2);
var YTDVsYTDTargetinPercentData = calculation4*100;
if (isNaN(YTDVsYTDTargetinPercentData) || YTDVsYTDTargetinPercentData == "Infinity"){
YTDVsYTDTargetinPercentData = 0;
}
var showroomCurrency = document.createElement('input');
showroomCurrency.name = "showroomCurrency_" + showrooms[i];
showroomCurrency.value = currencyData;
fullForm.appendChild(showroomCurrency);
var showroomNameField = document.createElement('input');
showroomNameField.name = "showroomname_" + showrooms[i];
showroomNameField.value = showroomname;
fullForm.appendChild(showroomNameField);
var tableColumnData = ['monthActual', 'monthTarget', 'priorYear', 'priorYearToDate', 'yearToDateTarget',
'yearToDateActual', 'YTDVsPYTDSalesCurrency', 'YTDVsPYTDSalesinPercent', 'YTDVsYTDTargetinSalesCurrency', 'YTDVsYTDTargetinPercent'];
for(var j=0; j<tableColumnData.length; j++){
var temp = document.createElement('input');
temp.name = tableColumnData[j]+ "_" + showrooms[i];
temp.value = (tableColumnData[j] +"Data");
fullForm.appendChild(temp);
}
}

You were really close, but just needed to take the next step. You need to store your variables in an array or an object so that you can dynamically reference them, rather than through literals.
In this example, I took your code and put all of the extra data into the otherData object. That allows you to give them human readable names, but also be able to use the square bracket notation to look up the property. i.e. otherData.monthActualData == otherData["monthActualData"].
No promises on whether or not I did this perfectly, but the concept is still there.
var fullForm = document.createElement('form');
fullForm.action = '/fpdf/requests/print_fullreport.php?year=' + requestYear + '&month=' + getMonth(requestMonth);
fullForm.id = 'fullForm';
fullForm.target = '_blank';
fullForm.method = 'post';
var showrooms = [1, 3, 4, 24, 27, 29, 34, 36, 37, 8, 21, 25, 26, 28, 31, 33, -1];
for (var i = 0; i <showrooms.length; i++){
var showroomData = allTargetsData.monthlyDetail[showrooms[i]];
var currencyData = showroomData.currency;
var showroomname = showroomData.showroom_name;
var otherData = {};
otherData.monthActualData = showroomData.total;
otherData.monthTargetData = Math.round(allTargetsData.originalTarget[requestYear][showrooms[i]][requestMonth]['amount']);
otherData.priorYearData = allTargetsData.realFigure[requestYear - 1][showrooms[i]]['figures'][requestMonth];
otherData.priorYearToDateData = showroomData.ly_ytd;
otherData.yearToDateTargetData = Math.round(showroomData.ytd_target);
otherData.yearToDateActualData = showroomData.ytd;
var calculation1 = (showroomData.ytd - showroomData.ly_ytd).toFixed(2);
otherData.YTDVsPYTDSalesCurrencyData = calculation1;
var calculation2 = (parseFloat(showroomData.ytd - showroomData.ly_ytd)/showroomData.ly_ytd).toFixed(2);
otherData.YTDVsPYTDSalesinPercentData = (calculation2*100);
if (isNaN(otherData.YTDVsPYTDSalesinPercentData) || otherData.YTDVsPYTDSalesinPercentData == "Infinity"){
otherData.YTDVsPYTDSalesinPercentData = 0;
}
var calculation3 = (showroomData.ytd - showroomData.ytd_target).toFixed(2);
otherData.YTDVsYTDTargetinSalesCurrencyData = (calculation3*100)/100;
var calculation4 = (parseFloat(showroomData.ytd - showroomData.ytd_target)/parseFloat(showroomData.ytd_target)).toFixed(2);
otherData.YTDVsYTDTargetinPercentData = calculation4*100;
if (isNaN(otherData.YTDVsYTDTargetinPercentData) || otherData.YTDVsYTDTargetinPercentData == "Infinity"){
otherData.YTDVsYTDTargetinPercentData = 0;
}
var showroomCurrency = document.createElement('input');
showroomCurrency.name = "showroomCurrency_" + showrooms[i];
showroomCurrency.value = currencyData;
fullForm.appendChild(showroomCurrency);
var showroomNameField = document.createElement('input');
showroomNameField.name = "showroomname_" + showrooms[i];
showroomNameField.value = showroomname;
fullForm.appendChild(showroomNameField);
var tableColumnData = ['monthActual', 'monthTarget', 'priorYear', 'priorYearToDate', 'yearToDateTarget',
'yearToDateActual', 'YTDVsPYTDSalesCurrency', 'YTDVsPYTDSalesinPercent', 'YTDVsYTDTargetinSalesCurrency', 'YTDVsYTDTargetinPercent'];
for(var j=0; j<tableColumnData.length; j++){
var temp = document.createElement('input');
temp.name = tableColumnData[j]+ "_" + showrooms[i];
temp.value = otherData[tableColumnData[j] +"Data"];
fullForm.appendChild(temp);
}
}

Related

Certain number question being missed in regex

I have the following if statement that removes the first instances of a number followed by the period. However, I am noticing it is missing to catch some of them (ex. "16.", "23.", "24.", etc.) and not sure why.
Here is the function:
function quesCleanUp(ques){
//Checks the first instance of "." and removes it and the number
if(ques.match(/[0-9]\./g)?.length > 1){//(ques.match(/./g)?.length > 1){
var quesClean = ques.replace(/^[^\.]*\./, '').trim();
} else{
var quesClean = ques.trim();
}
return quesClean;
}
The following for loop extracts the question from the google form:
for (var i = 0; i < items.length; i++) {
var item = items[i];
switch(item.getType()) {
case FormApp.ItemType.MULTIPLE_CHOICE:
var question = item.asMultipleChoiceItem();
var ques = quesCleanUp(question.getTitle().trim());//replace(/\s/g, "");
var question_type = "Multiple Choice";
var optns = [];
var answr;
var answers = question.getChoices();
answer_val = false;
for (var j = 0; j < answers.length; j++) {
var clean = answers[j].getValue().trim();
optns.push(clean);
if(answers[j].isCorrectAnswer()){
answr = answers[j].getValue().trim();
for(var x = 0; x < optns.length; x++){
if(answr == optns[x]){
answer_val = true;
break;
}
}
}
}
var multiJSON = makeJSON(ques, question_type, optns, answr);
console.log("JSON1: " + JSON.stringify(multiJSON));
constructedJSON[i+1] = multiJSON;
break;
case FormApp.ItemType.CHECKBOX:
var question = item.asCheckboxItem();
//var ques = question.getTitle().trim();//.replace(/\s/g, "");
var ques = quesCleanUp(question.getTitle().trim());//replace(/\s/g, "");
var question_type = "CheckBox";
var optns = [];
var answr = [];
var answers = question.getChoices();
for (var j = 0; j < answers.length; j++) {
var clean = answers[j].getValue().trim();//replace(/\s/g, "");
optns.push(clean);
if(answers[j].isCorrectAnswer()){
answr.push(answers[j].getValue().trim());
}
}
var checkJSON = makeJSON(ques, question_type, optns, answr);
console.log("JSON2: " + JSON.stringify(checkJSON));
constructedJSON[i+1] = checkJSON;
break;
case FormApp.ItemType.PARAGRAPH_TEXT:
var question = item.asParagraphTextItem();
//var ques = question.getTitle().trim();//.replace(/\s/g, "");
var ques = quesCleanUp(question.getTitle().trim());//replace(/\s/g, "");
var question_type = "free response";
var optns = [];
var answr;
var paraJSON = makeJSON(ques, question_type, optns, answr);
console.log("JSON3: " + JSON.stringify(paraJSON));
constructedJSON[i+1] = paraJSON;
break;
case FormApp.ItemType.TEXT:
var question = item.asTextItem();
//var ques = question.getTitle().trim();
var question_type = "free response";
var ques = quesCleanUp(question.getTitle().trim());//replace(/\s/g, "");
var optns = "";
var answr = "";
var textJSON = makeJSON(ques, question_type, optns, answr);
console.log("JSON4: " + JSON.stringify(textJSON));
constructedJSON[i+1] = textJSON;
break;
}
The following example is the type of question 16. What is the meaning of life?
And the expected output: What is the meaning of life?
Try using /[0-9]+./g to catch more than one digit
As a quick fix, in the function quesCleanUp() try to change the line:
if(ques.match(/[0-9]\./g)?.length > 1){//(ques.match(/./g)?.length > 1){
With:
if (ques.match(/^[0-9]+\./g).length > 0) {
I suspect you got the downvotes because you posted the code with glared typos. It looks like you didn't even try to debug it first. And as the icing on the cake you accepted a wrong answer.
And probably the function can be boiled down to just one line:
const quesCleanUp = q => q.replace(/^\d+\./,'').trim();
Here is how it works:
var questions = ['1. aaa', '16. What', '23. That', 'No nums'];
const quesCleanUp = q => q.replace(/^\d+\./,'').trim();
questions.forEach(q => console.log(quesCleanUp(q)));
Expected output:
aaa
What
That
No nums

appendchild is not working

I am working on nested array. When i am trying to insert data to a div by using appendChild It is throwing an error saying Cannot read property 'appendChild' of null
My Code goes hear
<script>
var emp1 = [];
emp1["Emsno"] = 10001;
emp1["name"] = "jack";
emp1 ["sall"] = 5000;
var emp2 = [];
emp2["Emsno"] = 10002;
emp2["name"] = "Reck";
emp2 ["sall"] = 5500;
var emp3 = [];
emp3["Emsno"] = 10003;
emp3["name"] = "lama";
emp3 ["sall"] = 5300;
var emp4 = [];
emp4["Emsno"] = 10004;
emp4["name"] = "sam";
emp4 ["sall"] = 6000;
var emps = [emp1, emp2, emp3, emp4];
var Employedisplay = document.getElementById("Employedisplay");
function showEmployes(){
var n = emps.length;
for (i = 0; i < n ; i++){
var emp = emps[i];
for(var key in emp){
var NewDiv = document.createElement("div");
NewDiv.innerHTML = key + ": " + emp[key];
Employedisplay.appendChild(NewDiv);
}
var NewBrk = document.createElement("br")
Employedisplay.appendChild(NewBrk);
}
}
</script>
</head>
<body>
<input type = "button" id = "MyArray" value ="Show Emps" onclick="showEmployes()"/>
<hr>
<div id="Employedisplay"></div>
</body>
and it is working in this way
function showEmployes(){
var n = emps.length;
for (i = 0; i < n ; i++){
var emp = emps[i];
for(var key in emp){
var NewDiv = document.createElement("div");
NewDiv.innerHTML = key + ": " + emp[key];
document.getElementById("Employedisplay").appendChild(NewDiv);
}
var NewBrk = document.createElement("br")
Employedisplay.appendChild(NewBrk);
}
}
I am not understanding where i am going wrong in my first approach?
The below line of code is executed even before the browser add the #Employedisplay element in DOM.
var Employedisplay = document.getElementById("Employedisplay");
So when you click the button, Employedisplay variable is null
It's better to bootstrap your code on page load, or you can get the #Employedisplay element at the start of showEmployee method.
<script>
var emp1 = [];
emp1["Emsno"] = 10001;
emp1["name"] = "jack";
emp1["sall"] = 5000;
var emp2 = [];
emp2["Emsno"] = 10002;
emp2["name"] = "Reck";
emp2["sall"] = 5500;
var emp3 = [];
emp3["Emsno"] = 10003;
emp3["name"] = "lama";
emp3["sall"] = 5300;
var emp4 = [];
emp4["Emsno"] = 10004;
emp4["name"] = "sam";
emp4["sall"] = 6000;
var emps = [emp1, emp2, emp3, emp4];
function bootstrap() {
var Employedisplay = document.getElementById("Employedisplay");
}
function showEmployes() {
var n = emps.length;
for (i = 0; i < n; i++) {
var emp = emps[i];
for (var key in emp) {
var NewDiv = document.createElement("div");
NewDiv.innerHTML = key + ": " + emp[key];
Employedisplay.appendChild(NewDiv);
}
var NewBrk = document.createElement("br")
Employedisplay.appendChild(NewBrk);
}
}
</script>
</head>
<body onload="bootstrap();">
<input type="button" id="MyArray" value="Show Emps" onclick="showEmployes()" />
<hr>
<div id="Employedisplay"></div>
</body>
<script>
var emp1 = [];
emp1["Emsno"] = 10001;
emp1["name"] = "jack";
emp1 ["sall"] = 5000;
var emp2 = [];
emp2["Emsno"] = 10002;
emp2["name"] = "Reck";
emp2 ["sall"] = 5500;
var emp3 = [];
emp3["Emsno"] = 10003;
emp3["name"] = "lama";
emp3 ["sall"] = 5300;
var emp4 = [];
emp4["Emsno"] = 10004;
emp4["name"] = "sam";
emp4 ["sall"] = 6000;
var emps = [emp1, emp2, emp3, emp4];
function showEmployes(){
var Employedisplay = document.getElementById("Employedisplay");
var n = emps.length;
for (i = 0; i < n ; i++){
var emp = emps[i];
for(var key in emp){
var NewDiv = document.createElement("div");
NewDiv.innerHTML = key + ": " + emp[key];
Employedisplay.appendChild(NewDiv);
}
var NewBrk = document.createElement("br")
Employedisplay.appendChild(NewBrk);
}
}
</script>
</head>
<body>
<input type = "button" id = "MyArray" value ="Show Emps" onclick="showEmployes()"/>
<hr>
<div id="Employedisplay"></div>
</body>

Javascript add row to HTML table with text and onClick

i am adding a new table row in javascript:
var i=1;
function addRow(seq, nominalcode, description, quantity, unitprice) {
seq = seq || '';
nominalcode = nominalcode || '';
description = description || '';
quantity = quantity || '';
unitprice = unitprice || '';
var tbl = document.getElementById('table1');
var lastRow = tbl.rows.length - 4;
//var iteration = lastRow - 1;
var row = tbl.insertRow(lastRow);
row.id = 'item_row_' + i;
var Cell0 = row.insertCell(0);
var elItemSequence = document.createElement('input');
elItemSequence.type = 'hidden';
elItemSequence.name = 'item_sequence' + i;
elItemSequence.id = 'item_sequence' + i;
elItemSequence.value = seq;
Cell0.appendChild(elItemSequence);
var elNominalcode = document.createElement('input');
elNominalcode.type = 'textarea';
elNominalcode.className = 'form-control';
elNominalcode.name = 'nominal_code' + i;
elNominalcode.id = 'nominal_code' + i;
elNominalcode.placeholder = 'Nominal Code';
elNominalcode.value = nominalcode;
Cell0.appendChild(elNominalcode);
var Cell1 = row.insertCell(1);
var elDescription = document.createElement('textarea');
elDescription.type = 'textarea';
elDescription.className = 'form-control';
elDescription.name = 'description' + i;
elDescription.id = 'description' + i;
elDescription.placeholder = 'Description';
elDescription.value = description;
elDescription.cols = 40;
elDescription.rows = 2;
Cell1.appendChild(elDescription);
var Cell2 = row.insertCell(2);
var elQuantity = document.createElement('input');
elQuantity.type = 'text';
elQuantity.className = 'form-control';
elQuantity.name = 'quantity' + i;
elQuantity.id = 'quantity' + i;
elQuantity.placeholder = 'Quantity';
elQuantity.value = quantity;
elQuantity.value = quantity;
Cell2 .appendChild(elQuantity);
var Cell3 = row.insertCell(3);
var elUnitPrice = document.createElement('input');
elUnitPrice.type = 'text';
elUnitPrice.className = 'form-control';
elUnitPrice.name = 'unitprice' + i;
elUnitPrice.id = 'unitprice' + i;
elUnitPrice.placeholder = 'Price';
elUnitPrice.value = unitprice;
Cell3.appendChild(elUnitPrice);
var Cell4 = row.insertCell(4);
var elDelete = document.createElement('a');
elDelete.href = '#';
elDelete.onclick = function(){ DeleteInvoiceLine(seq, i) };
elDelete.text = 'Delete' + i;
Cell4.appendChild(elDelete);
i++;
document.getElementById('numrows').value = (i-1);
//alert(i);
}
in the above code there is an onClick action which calls a function
but everytime the row is added its running the function, how can i make it only call the function on click of the a anchor
my function is:
function DeleteInvoiceLine(seq, row_num) {
alert(seq + '/' + row_num);
}
You can try anonymous function
elDelete.onclick = function(){ DeleteInvoiceLine(seq) };
Try this:
...
elDelete.setAttribute("onclick", "DeleteInvoiceLine("+ seq +","+ i +")");
....
There are several issues in your code.
1. elDelete.onclick=... not onClick
2. As you note DeleteInvoiceLine(seq) run at the moment the element created.
This is because you assign result of the function, not function to onclick event.
3. (not asked yet) What is sec variable? I suspect something like for(var sec=0;sec<N;sec++) and your code is inside the loop. This will not work (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures).
4. elDelete.onclick = function(){ DeleteInvoiceLine(seq) }; doesn't work because sec is out of scope at the moment of click.
Some fixes.
var i = 0;
//...
for(var sec=0;sec<N;sec++){
//your code
elDelete.onclick = DeleteInvoiceLine(seq, i);//I keep it with purpose
//your code
}
//...
i++;
//The key moment
function DeleteInvoiceLine(seq, row_num){
//sec comes here from the loop
return function(){//note (); (sec) would kill all construction
//and in this context would be **event {type:'click'}
$.ajax(
//your code which uses **sec** and/or **row_num**
//this time sec and row_num are available from parent scope
);//$.ajax
}//return function
}//function DeleteInvoiceLine

Javascript doesn't generate email

I've coded some js into an Articulate Storyline file for a workshop. I'm trying to get the system to compile a number of variables and email the data to the user. When I click on the icon to generate the email I get nothing.
SET DATE CODE
var d = new Date();
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthName = new Array("January","February","March","April","May","June","July","August","Septemeber","October","November","December");
var dateVal = d.getDate().toString();
var delimeter = ", ";
var delimeter1 = " ";
var txtPostdate = ""
var lDigit = dateVal.charAt( dateVal.length-1);
if (lDigit = = "1"){
txtPostdate = "st";
}else if(lDigit = = "2"){
txtPostdate = "nd";
}else if(lDigit = = "3"){
txtPostdate = "rd";
}else{
txtPostdate = "th";
}
var dateString = weekday[d.getDay()]+delimeter+monthName[(d.getMonth())]+delimeter1+dateVal+txtPostdate+delimeter+d.getFullYear();
var player = GetPlayer();
player.SetVar("SystemDate",dateString);
SEND EMAIL CODE
var player = GetPlayer();
var useremail = player.GetVar("email");
var subject ="My Fact Sheet - Optimizing Your Online Job Applications";
var firstname = player.GetVar("firstname");
var lastname = player.GetVar("lastname");
var street = player.GetVar("street1");
var address2 = player.GetVar("street2");
var city = player.GetVar("city");
var state = player.GetVar("state");
var zip = player.GetVar("zip");
var phone = player.GetVar("phone");
var cell = player.GetVar("cell");
var mymail = player.GetVar("mymail");
var phone = player.GetVar("phone");
var cell = player.GetVar("cell");
var school1 = player.GetVar("school1");
var s1city = player.GetVar("s1city");
var s1degree = player.GetVar("s1degree");
var s1date = player.GetVar("s1date");
var s1start = player.GetVar("s1start");
var school2 = player.GetVar("school2");
var s2city = player.GetVar("s2city");
var s2degree = player.GetVar("s2degree");
var s2start = player.GetVar("s2start");
var s2date = player.GetVar("s2date");
var school3 = player.GetVar("school3");
var s3city = player.GetVar("s3city");
var s3degree = player.GetVar("s3degree");
var s3date = player.GetVar("s3date");
var s3start = player.GetVar("s3start");
var e1title = player.GetVar("e1title");
var e1start = player.GetVar("e1start");
var e1end = player.GetVar("e1end");
var e1 = player.GetVar("e1");
var e1employer = player.GetVar("e1employer");
var e1city = player.GetVar("e1city");
var e1state = player.GetVar("e1state");
var e1zip = player.GetVar("e1zip");
var e1super = player.GetVar("e1super");
var e1phone = player.GetVar("e1phone");
var e1reason = player.GetVar("e1reason");
var e1startsalary = player.GetVar("e1startsalary");
var e1endsalary = player.GetVar("e1endsalary");
var e1accomplish = player.GetVar("e1accomplish");
var e2title = player.GetVar("e2title");
var e2start = player.GetVar("e2start");
var e2end = player.GetVar("e2end");
var e2 = player.GetVar("e2");
var e2employer = player.GetVar("e2employer");
var e2city = player.GetVar("e2city");
var e2state = player.GetVar("e2state");
var e2zip = player.GetVar("e2zip");
var e2super = player.GetVar("e2super");
var e2phone = player.GetVar("e2phone");
var e2reason = player.GetVar("e2reason");
var e2startsalary = player.GetVar("e2startsalary");
var e2endsalary = player.GetVar("e2endsalary");
var e2accomplish = player.GetVar("e2accomplish");
var e3title = player.GetVar("e3title");
var e3start = player.GetVar("e3start");
var e3end = player.GetVar("e3end");
var e3 = player.GetVar("e3");
var e3employer = player.GetVar("e3employer");
var e3city = player.GetVar("e3city");
var e3state = player.GetVar("e3state");
var e3zip = player.GetVar("e3zip");
var e3super = player.GetVar("e3super");
var e3phone = player.GetVar("e3phone");
var e3reason = player.GetVar("e3reason");
var e3startsalary = player.GetVar("e3startsalary");
var e3endsalary = player.GetVar("e3endsalary");
var e3accomplish = player.GetVar("e3accomplish");
var r1name = player.GetVar("r1name");
var r1company = player.GetVar("r1company");
var r1email = player.GetVar("r1email");
var r1phone = player.GetVar("r1phone");
var r2name = player.GetVar("r2name");
var r2company = player.GetVar("r2company");
var r2email = player.GetVar("r2email");
var r2phone = player.GetVar("r2phone");
var r3name = player.GetVar("r3name");
var r3company = player.GetVar("r3company");
var r3email = player.GetVar("r3email");
var r3phone = player.GetVar("r3phone");
var cover = player.GetVar("cover");
var resume = player.GetVar("resume");
var mail = player.GetVar("mail");
var mailto_link='mailto:'+useremail+'?subject='+subject+'&body='+"Activity Notes – Optimizing Your Online Job Applications %0d%0A %0d%0AMy Name:%0d%0A”+firstname+”%0d%0A”+lastname+”%0d%0A%0d%0AMy Address:%0d%0A”+street1+”%0d%0A”+street2+”%0d%0A”+city+”%0d%0A”+state+”%0d%0A”+zip+”%0d%0A%0d%0AMy Home Phone: %0d%0A"+phone+”%0d%0A%0d%0AMy Cell Phone: %0d%0A”+cell+”%0d%0A%0d%0AMy eMail: %0d%0A "+mymail+”%0d%0A%0d%0AMy Education:%0d%0A”+school1+”%0d%0A”+s1city+”+s1degree+”%0d%0A”+s1start+”%0d%0A”+s1date+”%0d%0A%0d%0A”+school2+”%0d%0A”+s2city+”%0d%0A”+s2degree+”%0d%0A“+s1start+”%0d%0A”+s2date+”%0d%0A%0d%0A”+school3+”%0d%0A“+s3city+”%0d%0A“+s3degree+”%0d%0A“+s1start+”%0d%0A“+s3date+”%0d%0A%0d%0AMy Employment History - Job 1: %0d%0A“+e1title+”%0d%0A%0d%0ADates: %0d%0A “+e1start+”%0d%0A“+e1end+”%0d%0A%0d%0AEmployer Information:%0d%0A“+e1+”%0d%0A“+e1employer+”%0d%0A“+e1city+”%0d%0A“+e1state+”%0d%0A“+e1zip+”%0d%0A%0d%0ACompany Contact: %0d%0A“+e1super+”%0d%0A“+e1phone+”%0d%0A%0d%0AReason for Leaving: %0d%0A“+e1reason+”%0d%0A%0d%0AStarting Salary: “+e1startsalary+”%0d%0AEnding Salary: ”+e1endsalary+”%0d%0A%0d%0AAccomplishments:%0d%0A“+e1accomplish+”%0d%0A%0d%0AMy Employment History - Job 2:%0d%0A“+e2title+”%0d%0A%0d%0ADates:%0d%0A“+e2start+”%0d%0A“+e2end+”%0d%0A%0d%0AEmployer Information: %0d%0A“+e2+”%0d%0A“+e2employer+”%0d%0A“+e2city+”%0d%0A“+e2state+”%0d%0A“+e2zip+”%0d%0A%0d%0ACompany Contact: %0d%0A“+e2super+”%0d%0A“+e2phone+”%0d%0A%0d%0AReason for Leaving:%0d%0A“+e2reason+”%0d%0A%0d%0AStarting Salary: “+e2startsalary+”%0d%0AEnding Salary: “+e2endsalary+”%0d%0A%0d%0AAccomplishments:%0d%0A“+e2accomplish+”%0d%0A%0d%0AMy Employment History - Job 3:%0d%0A“+e3title+”%0d%0A%0d%0ADates:%0d%0A“+e3start+”%0d%0A“+e3end+”%0d%0A%0d%0AEmployer Information:%0d%0A
“+e3+”%0d%0A“+e3employer+”%0d%0A“+e3city+”%0d%0A“+e3state+”%0d%0A“+e3zip+”%0d%0A%0d%0ACompany Contact:%0d%0A“+e3super+”%0d%0A“+e3phone+”%0d%0A%0d%0AReason for Leaving:%0d%0A“+e3reason+”%0d%0A%0d%0AStarting Salary: “+e3startsalary+”%0d%0AEnding Salary: “+e3endsalary+”%0d%0A%0d%0AAccomplishments:%0d%0A“+e3accomplish+”%0d%0A%0d%0AMy References:%0d%0A“+r1name+”%0d%0A“+r1company+”%0d%0A“+r1email+”%0d%0A“+r1phone+”%0d%0A%0d%0A“+r2name+”%0d%0A“+r2company+”%0d%0A“+r2email+”%0d%0A“+r2phone+”%0d%0A%0d%0A“+r3name+”%0d%0A“+r3company+”%0d%0A“+r3email+”%0d%0A“+r3phone+”%0d%0A%0d%0AMy Resume:%0d%0A“+resume+”%0d%0A%0d%0AMy Cover Letter:%0d%0A“+cover+”%0d%0A%0d%0A
win=window.open(mailto_link,'emailWin');
Try putting %20 in place of spaces. Text in the submitted URL has spaces "Activity Notes – Optimizing Your Online.........". I did not survey the entire URL, but be sure to encode wherever needed.

Javascript/DOM: On a dynamically created form text object, how do you reference the value via DOM?

I have an array that looks like this:
grades[0] = ["A", 4.0, 0, "a"];
grades[1] = ["A-", 3.67, 0, "a-minus"];
grades[2] = ["B+", 3.33, 0, "b-plus"];
grades[3] = ["B", 3.0, 0, "b"];
grades[4] = ["B-", 2.67, 0, "b-minus"];
grades[5] = ["C+", 2.33, 0, "c-plus"];
grades[6] = ["C", 2.0, 0, "c"];
grades[7] = ["C-", 1.67, 0, "c-minus"];
grades[8] = ["D+", 1.33, 0, "d-plus"];
grades[9] = ["D", 1.0, 0, "d"];
grades[10] = ["D-", 0.67, 0, "d-minus"];
grades[11] = ["F", 0.0, 0, "f"];
and a dynamic form that looks like this:
function loadButtons() {
var myDiv = document.getElementById("divMain");
var myTable = document.createElement("table");
myTable.id = "grades";
myTable.border = 1;
myTable.padding = 5;
var myTbody = document.createElement("tbody");
for (var i=0; i < grades.length; i++) {
var myTr = document.createElement("tr");
var myTd1 = document.createElement("td");
var myTd2 = document.createElement("td");
var myTd3 = document.createElement("td");
var myTd4 = document.createElement("td");
var myButton = document.createElement('input');
myButton.type = 'button';
myButton.name = grades[i][1];
myButton.value = "+";
myButton.onclick = function (){addGrade(this)};
myTd1.appendChild(myButton);
myTr.appendChild(myTd1);
myTd2.appendChild(document.createTextNode(grades[i][1]));
myTr.appendChild(myTd2);
myTd3.appendChild(document.createTextNode(grades[i][2]));
myTr.appendChild(myTd3);
var myButton = document.createElement('input');
myButton.type = 'text';
myButton.name = grades[i][1];
myButton.id = grades[i][3];
myButton.size = "4";
myButton.onblur = function (){addGradeX(this)};
myTd4.appendChild(myButton);
myTr.appendChild(myTd4);
myTbody.appendChild(myTr);
myTable.appendChild(myTbody);
myDiv.appendChild(myTable);
}
}
And I'm trying to update the value of the button in Td4 (the last button) using this:
function writeGrades() {
clearForm();
for (var x=0; x < grades.length; x++) {
if (grades[x][2] > 0) {
for (var i=1; i <= grades[x][2]; i++) {
sGradeLog += grades[x][0] + ",";
sGradeValLog += grades[x][1] + ",";
iCumTotal += grades[x][1];
iNumOfGrades += 1;
}
}
}
for (var x=0; x < grades.length; x++) { //**this loop is where I'm trying to make this happen**
var myVal = grades[x][3];
var myEl = getElementById(document.form.myVal.value);
document.form.myEl.value = grades[x][2];
}
iGPA = iCumTotal / iNumOfGrades;
document.form.GPA.value = iGPA.toFixed(3);
document.form.cumTotal.value = iCumTotal.toFixed(3);
document.form.numOfGrades.value = iNumOfGrades;
document.form.gradeLog.value = sGradeLog;
document.form.gradeValLog.value = sGradeValLog;
}
I have tried many different ways of access the document.form.grades[x][3].value DOM object but it doesn't like the array reference not being an object. My most recent attempt was adding the .id value to the button and then trying to use getElementById() but that didnt' work either.
I'm a novice at JS so please assist where you can. TIA!
Like this : document.form[grades[x][3]].value ?

Categories

Resources