SetValues from array rearranged - javascript
General View
https://docs.google.com/spreadsheets/d/e/2PACX-1vSNW9Gibu5OSFs7Os73vbfwtloJnyw4PnzPLs9IXGzDSshC2Y10jxKHChDYpScJTfHf2viqAADwPmya/pubhtml
after editing previous code thanks to theMaster now I have:
function layout_inc() {
var semanal = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Semanal');
semanal.getRange('D4').clearcontent;
var rangoEmpleados = semanal.getRange(3, 1, semanal.getLastRow() - 2, 10).getDisplayValues();
var ly_inc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Layout_incidencias');
ly_inc.clearContents();
var rangely_inc = ly_inc.getRange(1, 1, rangoEmpleados.length, 1);
for (let i = 0; i < rangoEmpleados.length; i++) {
var innerArrayLength = rangoEmpleados[i].length;
for (let j = 3; j < innerArrayLength; j++) {
let outputArray = [];
if (rangoEmpleados[i][j] == 'F') {
outputArray = '"' + rangoEmpleados[i][0] + '"' + ',' + rangoEmpleados[0][j] + ',' + ',' + '"F"' + "," + '""' + "," + '""' + "," + '""'
rangely_inc.setValue(outputArray);
SpreadsheetApp.flush()
Logger.log(outputArray);
} else if (rangoEmpleados[i][j] == 'V') {
var v = [ '"' + rangoEmpleados[i][0] + '"' + "," + rangoEmpleados[0][j] + '""' + ',' + '"V"' + "," + '""' + "," + '""' + "," + '""'];
}
}
}
}
So in order to create a new array that shows the emp_id (A4:A), date(C3:I3) and a string "F", if and only if theres an F where there isnt any clockin/out movement, Im trying to setvalues in a different sheet and that sheet will be a csv later. But, setValue will only return a string, the very first one and duplicate it trough the entire array. yet
Logger.log(outputArray); it does show the new array how I want it, I just need to set it in the other sheet.
4:19:22 AM Info "547",20/06/2022,,"F","","",""
4:19:23 AM Info "855",17/06/2022,,"F","","",""
4:19:23 AM Info "855",18/06/2022,,"F","","",""
4:19:23 AM Info "855",20/06/2022,,"F","","",""
4:19:24 AM Info "855",21/06/2022,,"F","","",""
4:19:24 AM Info "855",22/06/2022,,"F","","",""
4:19:24 AM Info "855",23/06/2022,,"F","","",""
but:
rangely_inc.setValue(outputArray);
returns:
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
"507",18/06/2022,,"F","","",""
Treat the data as an array.
Every spreadsheet in Google Sheets is treated as a 2D array. In this solution, I introduced a secondary output variable out2 and pushed the desired outputs into it. Afterwards, I declared the rangely_inc variable last so that its range may adjust dynamically to the size of out2. With that, there will then be no issues in using the .setValues() function.
function layout_inc() {
var semanal = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Semanal');
semanal.getRange('D4').clearcontent;
var rangoEmpleados = semanal.getRange(3, 1, semanal.getLastRow() - 2, 10).getDisplayValues();
var ly_inc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Layout_incidencias');
ly_inc.clearContents();
var outputArray = [];
var out2 = [];
for (let i = 0; i < rangoEmpleados.length; i++) {
var innerArrayLength = rangoEmpleados[i].length;
for (let j = 3; j < innerArrayLength; j++) {
if (rangoEmpleados[i][j] == 'F') {
outputArray= ['"' + rangoEmpleados[i][0] + '"' + ',' + rangoEmpleados[0][j] + ',' + ',' + '"F"' + "," + '""' + "," + '""' + "," + '""'];
out2.push(outputArray);
} else if (rangoEmpleados[i][j] == 'V') {
var v = ['"' + rangoEmpleados[i][0] + '"' + "," + rangoEmpleados[0][j] + '""' + ',' + '"V"' + "," + '""' + "," + '""' + "," + '""'];
}
}
}
var rangely_inc = ly_inc.getRange(1,1, out2.length, 1);
rangely_inc.setValues(out2);
}
Related
Done / How to make a space printer in javascript
var arg = 5 var string = ' ' for (let i = 0; i < arg; i++) { console.log('"' + string + '"') } I expected the output is: " " \\ There are 5 spaces between the "" But the output is: " " " " " " " " " " I am a newbie in javascript. Hope you will help me
you can use method string.padEnd that fill string with blank space until parameter pass to the method var arg = 5; var string = ' '; string = string.padEnd(arg) console.log('"' + string + '"'); reference : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
I don't think there is a way to console.log on the same line. You can do something like this var arg = 5 var string = ' ' var final_string = '' for (let i = 0; i < arg; i++) final_string = final_string + string } console.log('"' + final_string + '"')
I think you can use or \xa0 for printing spaces var arg = 5 var string = '\xa0\xa0\xa0\xa0\xa0'; for (let i = 0; i < arg; i++) { console.log('"' + string + '"') }
There are many ways to do this. One would be to do it via a loop. That way you can stay flexible. For loop has already been mentioned. here with a foreach loop. let string = ''; [...Array(5).keys()].forEach(e => string += ' '); console.log('"' + string + '"');
How can I make this XMLHttpRequest for Leaflet more simple? Just vanilla JS
Can this be better? More simple? My Steps: Created a Google Doc Sheet -spread sheet Exported as json Build this ajax js script to make available to leaflet. js app. https://jsfiddle.net/lukedohner/by80bvL7/ There maybe a more succinct way. xhr.onload = function() { console.log("onload + function"); if (xhr.status === 200) { //sucessfull load of json alert("Here is the data in a alert window " + xhr.responseText); var respText = xhr.responseText; xhrText = JSON.parse(respText); // convert it to an object console.log( "json data: xhrText.length >>>>>>>> " + Object.keys(xhrText.mysheet).length ); console.log( "Object.keys(xhrText) >>>>>>>> " + Object.keys(xhrText) ); var i = -1; for (var mykey in xhrText.mysheet) { //In case you want to check to see it there is a null value -property if (xhrText.mysheet.hasOwnProperty(mykey)) { i++; window["card" + i] = mykey + " -> " + xhrText[mykey]; } } } else { // not sucessfull load of json alert("Request failed. Returned status of " + xhr.status); } createhooks(); }; }; var ch = {}; // create a var namespace createhooks = function() { var mysheetlenght = Object.keys(xhrText.mysheet).length; for (var j = 0; j < mysheetlenght; j++) { //console.log(" xhrText.title >>>>>>>> " + j + " " + xhrText.mysheet[j].title); ch["title" + j] = xhrText.mysheet[j].title; ch["subtitle" + j] = xhrText.mysheet[j].subtitle; ch["copy" + j] = xhrText.mysheet[j].copy; ch["imagename" + j] = xhrText.mysheet[j].imagename; console.log("title" + j + " is " + ch["title" + j]); console.log("subtitle" + j + " is " + ch["subtitle" + j]); console.log("copy" + j + " is " + ch["copy" + j]); console.log("imagename" + j + " is " + ch["imagename" + j]); console.log("~~~~~ " + "~~~~~ "); //create vars for addLElement function - Display it is the DOM titleindex = "ch.title" + j; addElement("Title " + j, ch["title" + j]); subtitleindex = ["ch.subtitle" + j]; addElement("Subtitle " + j, ch["subtitle" + j]); copyindex = ["ch.copy" + j]; addElement("Copy " + j, ch["copy" + j]); imagenameindex = ["ch.imagename" + j]; addElement("Image Name " + j, ch["imagename" + j]); addElement("~~~~~ ", "~~~~~"); } ch_callback(); }; function ch_callback() { // Use ch.title1, ch.title1, ch.title1, ch.title1, ch.title1... in your add addElement function or in the Leaflet.js L.control.window method console.log("callback " + ch.copy1); } Now I can use the var like this ch.copy1 in the leaflet L.control.window method. Not shown in fiddle example.
Generate random equations with random numbers [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago. Improve this question I want to generate random equations in JavaScriptp and then output them into an HTML tag. This is the code: <!DOCTYPE html> <body> <p>Click the button below to generate a random equation.</p> <button onclick="change();">Generate</button> <p id="generate"></p> <script> function getRandomizer(bottom, top) { return function() { return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom; } } function getRandomNumber(results) { var rollDie = getRandomizer( 1, 10 ); for ( var i = 0; i < 3; i++ ) { results += rollDie() + ""; } getRandomNumber.result = results; } function getRandomEquation(num1, num2, num3, num4, num5, num6, num7, output) { var num_7, num_6, num_5, num_4, num_3, num_2, num_1 getRandomNumber(num1).result = num_7; getRandomNumber(num2).result = num_6; getRandomNumber(num3).result = num_5; getRandomNumber(num4).result = num_4; getRandomNumber(num5).result = num_3; getRandomNumber(num6).result = num_2; getRandomNumber(num7).result = num_1; var equation1 = "" + num_1 + " x " + num_2 + " + {" + num_3 + " x [(" + num_4 + " x " + num_5 + ") - " + num_6 + "] + " + num_7 + "} = x", equation2 = "" + num_1 + " x " + num_2 + " = y", equation3 = "" + num_1 + "s x " + num_2 + " = z, s = " + num_3, equation4 = "" + num_1 + " + {" + num_2 + " x [" + num_3 + " + (" + num_4 + " x " + num_5 + ") + " + num_6 + "] + " + num_7 + "} = x", equation5 = "" + num_1 + "e + " + num_2 + "l x " + num_3 + " + " + num_4 + "a, e = " + num_5 + ", l = " + num_6 + ", a = " + num_7, equation6 = "[" + num_1 + " x " + num_2 + "z] + {" + num_3 + " - " + num_4 + "} + (" + num_5 + " + " + num_6 + ") = e, z = " + num_7, equation7 = "p" + " x " + num_1 + " / " + num_2 + " - " + num_3 + " + " + num_4 + " = e, p = " + num_5 var values = [ // there is an easier way to do this, too lazy "" + equation1, "" + equation2, "" + equation3, "" + equation4, "" + equation5, "" + equation6, "" + equation7 ] var i = 0; var e; if (i > values.length) { i = 0; } var randomEquation = values[i]; i++; e = values[i]; this.output = randomEquation; this.e = e; } function getEquation() { var bl1, bl2, bl3, bl4, bl5, bl6, bl7, equationOutput; var eq = getRandomEquation(bl1, bl2, bl3, bl4, bl5, bl6, bl7, equationOutput).e; getEquation.equation = eq; } function change() { var final = getEquation().equation; document.getElementById("generate").innerHTML = final; } </script> </body> </html> But it dosen't work. Any help? P.S. My teacher assigned this to me. Please respond as soon as possible. Thanks.
This code is a complete mess. I dont know where it comes from, but definitely not Javascript. Try the following instead: <!DOCTYPE html> <body> <p>Click the button below to generate a random equation.</p> <button onclick="change();">Generate</button> <p id="generate"></p> <script> function getRandomizer(bottom, top) { return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom; } function getRandomNumber() { var results=""; for ( var i = 0; i < 3; i++ ) { results += getRandomizer( 1, 10 ); } return results; } function getRandomEquation() { var num_7 = getRandomNumber(), num_6 = getRandomNumber(), num_5 = getRandomNumber(), num_4 = getRandomNumber(), num_3 = getRandomNumber(), num_2 = getRandomNumber(), num_1 = getRandomNumber(); var equation1 = num_1+" x "+num_2+" + {"+num_3+" x [("+num_4+" x "+num_5+") - "+num_6+"] + "+num_7+"} = x", equation2 = num_1+" x "+num_2+" = y", equation3 = num_1+"s x "+num_2+" = z, s = "+num_3, equation4 = num_1+" + {" +num_2+ " x [" +num_3+" + ("+num_4+" x "+num_5+") + "+num_6+"] + "+num_7+"} = x", equation5 = num_1+"e + "+num_2+"l x "+num_3+" + "+num_4+"a, e = "+num_5+", l = "+num_6+", a = "+ num_7, equation6 = "["+num_1+" x "+num_2+ "z] + {"+num_3+" - "+num_4+"} + ("+num_5+" + "+num_6+") = e, z = "+ num_7, equation7 = "p x "+num_1+" / "+num_2+" - "+num_3+" + "+num_4+" = e, p = "+num_5 var randomEquation = [ equation1, equation2, equation3, equation4, equation5, equation6, equation7 ] return randomEquation.join("<br>"); } function change() { document.getElementById("generate").innerHTML = getRandomEquation(); } </script> </body> </html>
Well, you could do this: var type = (Math.floor(Math.random(4)); var ret = 0; var n = [(Math.floor(Math.random(100)), (Math.floor(Math.random(100))]; if (type == 0) ret = n[0] + n[1] else if (type == 1) ret = Math.abs(n[0] - n[1]) else if (type == 2) ret = n[0] * n[1]; else if (type == 3) ret = n[0] / n[1] else ret = n[0] / 5 % n[1] // do something with ret It's fully expandable, just edit n and the if statements
HTML button action not working in Safari [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 9 years ago. Improve this question I'm running a web page in Safar, however; I notice that when I press the save button the event doesn't fire. It works fine in IE though. I've researched the issue, and found that it's not due to a missing value attribute or single/double quote specifics. Any help would be appreciated. <input type='button' name='Save' id='saveCut' value='Save Cut' class=button onClick=\"Puma.saveTheCut()\"> JS function Puma.saveTheCut = function () { var offerId = inStoreCut.cutOfferFields[0]; var merchId = inStoreCut.cutOfferFields[1]; var adId = inStoreCut.cutOfferFields[2]; var eventID = inStoreCut.cutOfferFields[3]; var adNum = inStoreCut.cutOfferFields[4]; var cutID = inStoreCut.cutOfferFields[5]; var merchDescription = parent.main.document.getElementById('merchDescription').value; var UPC = parent.main.document.getElementById('merchUPC').value; var nrfSampleColorObj = parent.main.document.getElementById('itemColor'); var nrfSampleColor = nrfSampleColorObj.options[nrfSampleColorObj.selectedIndex].value; var nrfsampleSubColorObj = parent.main.document.getElementById('itemChildColor'); var colorCodeStr = nrfsampleSubColorObj.options[nrfsampleSubColorObj.selectedIndex].value; var nrfsampleSubColor = colorCodeStr.substring(0, 3); var customer_Facing_Color = parent.main.document.getElementById('merchCustomerFacingColor').value; var division = parent.main.document.getElementById('merchFob').value; var deptNum = parent.main.document.getElementById('merchDept0~0').value; var vendorNum = parent.main.document.getElementById('merchVendorNum0~0').value; var pID = parent.main.document.getElementById('pID0~0').value; var regPrice = parent.main.document.getElementById('regPrice').value; var sampleSize = parent.main.document.getElementById('itemSize').value; var itemQty = parent.main.document.getElementById('itemQty').value; if (parent.main.document.getElementById('chkMerchSet').checked) { var set = "1"; } else { var set = "0"; } var sampleTypeObj = parent.main.document.getElementById("itemType"); var sampleType = sampleTypeObj.options[sampleTypeObj.selectedIndex].text; var merchColorCorrObj = parent.main.document.getElementById("merchColorCorr"); var colorCorr = merchColorCorrObj.options[merchColorCorrObj.selectedIndex].value; var merchSwatchObj = parent.main.document.getElementById("merchSwatch"); var Swatch = merchSwatchObj.options[merchSwatchObj.selectedIndex].value; var pantoneColor = parent.main.document.getElementById('merchPantone').value; var photoStylingDetails = parent.main.document.getElementById('merchPhotoStylingDetails').value; var mCOMSampleId = parent.main.document.getElementById('mCOMSample').value; var deptName = parent.main.document.getElementById('merchDeptName0~0').innerHTML; var vendorName = Puma.decoder(parent.main.document.getElementById('merchVendorName0~0').innerHTML); if (parent.main.document.getElementById('pidStatus0~0').value == "NOT IN PD") { var pidStatus = "0"; } else { var pidStatus = "1"; } var pidDescription = parent.main.document.getElementById('pidDescription').value; var webId = parent.main.document.getElementById('webID0~0').innerHTML; var vStyle = parent.main.document.getElementById('merchVStyle').value; var markStyle = parent.main.document.getElementById('merchMarkStyle').value; var subClass = parent.main.document.getElementById('Subclass').value; // var productDescription = parent.main.document.getElementById('productDescription').value; var docLineitemNum = parent.main.document.getElementById('merchDoc').value; var merchTurnInStatusObj = parent.main.document.getElementById("merchTurnInStatus"); var turnInStatus = merchTurnInStatusObj.options[merchTurnInStatusObj.selectedIndex].text; var reason = parent.main.document.getElementById('merchReason').value; var merchCountryOriginObj = parent.main.document.getElementById("countryOfOrigin"); var countryOfOrigin = merchCountryOriginObj.options[merchCountryOriginObj.selectedIndex].value; var importedCountry = parent.main.document.getElementById("importedCountries").value; //var importedCountry = merchImportedCountryObj.options[merchImportedCountryObj.selectedIndex].text; var fabricContent = parent.main.document.getElementById("fabricContent").value; var careInstructions = parent.main.document.getElementById("careInstructions").value; var offerDescription = parent.main.document.getElementById("offerDescription").value; var onlyAtMacysObj = parent.main.document.getElementById("onlyAtMacys"); var onlyAtMacysValue = parseInt(onlyAtMacysObj.options[onlyAtMacysObj.selectedIndex].value, 10); var onlyAtMacys = onlyAtMacysValue; var legalOneObj = parent.main.document.getElementById("legalOne"); var legalOne = legalOneObj.options[legalOneObj.selectedIndex].value; var legalOneExplain = parent.main.document.getElementById("explainLegalOne").value; var legalTwoObj = parent.main.document.getElementById("legalTwo"); var legalTwo = legalTwoObj.options[legalTwoObj.selectedIndex].value; var legalTwoExplain = parent.main.document.getElementById("explainLegalTwo").value; var legalThreeObj = parent.main.document.getElementById("legalThree"); var legalThree = legalThreeObj.options[legalThreeObj.selectedIndex].value; var legalThreeExplain = parent.main.document.getElementById("explainLegalThree").value; var legalFourObj = parent.main.document.getElementById("legalFour"); var legalFour = legalFourObj.options[legalFourObj.selectedIndex].value; var fiftyObj = parent.main.document.getElementById("overFifty"); var fifty = fiftyObj.options[fiftyObj.selectedIndex].value; var userId = parent.botnav.uinfo.userID; if (Puma.btiRequiredFieldIsValidated() == true) { if (inStoreCut.existingRecord == false) { sql = "action=saveMerchFormForCut&cutID=" + cutID + //sql = "action=updateMerchFormForCut&cutID=" + cutID + "&merchDescription=" + encodeURIComponent(merchDescription) + "&UPC=" + UPC + "&nrfSampleColor=" + nrfSampleColor + "&nrfSampleSubColor=" + nrfsampleSubColor + "&division=" + encodeURIComponent(division) + "&deptNum=" + deptNum + "&merchVendorNum=" + vendorNum + "&pID=" + pID + "&Customer_Facing_Color=" + encodeURIComponent(customer_Facing_Color) + "®Price=" + regPrice + "&sampleSize=" + encodeURIComponent(sampleSize) + "&itemQty=" + itemQty + "&set=" + set + "&sampleType=" + sampleType + "&colorCorr=" + colorCorr + "&Swatch=" + Swatch + "&pantoneColor=" + encodeURIComponent(pantoneColor) + "&photoStylingDetails=" + encodeURIComponent(photoStylingDetails) + "&mCOMSampleId=" + mCOMSampleId + "&deptName=" + deptName + "&vendorName=" + encodeURIComponent(vendorName) + "&pidStatus=" + pidStatus + "&pidDescription=" + pidDescription + "&webId=" + webId + "&vStyle=" + vStyle + "&markStyle=" + markStyle + "&subClass=" + subClass + "&docLineItemNum=" + docLineitemNum + "&merchTurnInStatus=" + turnInStatus + "&reason=" + encodeURIComponent(reason) + "&countryOfOrigin=" + countryOfOrigin + "&importedCountry=" + importedCountry + "&fabricContent=" + encodeURIComponent(fabricContent) + "&careInstructions=" + encodeURIComponent(careInstructions) + "&offerDescription=" + encodeURIComponent(offerDescription) + "&onlyAtMacys=" + onlyAtMacys + "&legalOne=" + legalOne + "&legalOneExplain=" + legalOneExplain + "&legalTwo=" + legalTwo + "&legalTwoExplain=" + legalTwoExplain + "&legalThree=" + legalThree + "&legalThreeExplain=" + legalThree + "&legalFour=" + legalFour + "&fifty=" + fifty + "&createdBy=" + userId var ajaxMaster = new AjaxMaster(sql, "Puma.saveMerchFormForCutData(data)", "", "btiDispatcher.aspx"); sql = "action=updateMerchFormForCut&sql=" + encodeURIComponent(msql); objAjaxAd.main_flag = "updateMerchFormForCut"; objAjaxAd.SendQuery(sql); // "[t0].[signedByUserID]," + // "[t0].[signedStatus]," + //"[t0].[dateSigned]," + //"[t0].[signedLastByUserID]," + //"[t0].[dateLastSigned1]," + //"[t0].[signedLastStatus]" + // var ajaxMaster = new AjaxMaster(sql, "Puma.updateMerchFormForCutData(data)", "", "puma_core.aspx"); } else { sql = "action=updateMerchFormForCut&cutID=" + cutID + "&offerId" + offerId + "&merchId" + merchId + "&adID" + adId + "&eventID" + eventID + "&adNum" + adNum + "&merchDescription=" + encodeURIComponent(merchDescription) + "&UPC=" + UPC + "&nrfSampleColor=" + nrfSampleColor + "&nrfSampleSubColor=" + nrfsampleSubColor + "&division=" + encodeURIComponent(division) + "&deptNum=" + deptNum + "&merchVendorNum=" + vendorNum + "&pID=" + pID + "&Customer_Facing_Color=" + encodeURIComponent(customer_Facing_Color) + "®Price=" + regPrice + "&sampleSize=" + encodeURIComponent(sampleSize) + "&itemQty=" + itemQty + "&set=" + set + "&sampleType=" + sampleType + "&colorCorr=" + colorCorr + "&Swatch=" + Swatch + "&pantoneColor=" + encodeURIComponent(pantoneColor) + "&photoStylingDetails=" + encodeURIComponent(photoStylingDetails) + "&mCOMSampleId=" + mCOMSampleId + "&deptName=" + deptName + "&vendorName=" + encodeURIComponent(vendorName) + "&pidStatus=" + pidStatus + "&pidDescription=" + pidDescription + "&webId=" + webId + "&vStyle=" + vStyle + "&markStyle=" + markStyle + "&subClass=" + subClass + "&docLineItemNum=" + docLineitemNum + "&merchTurnInStatus=" + turnInStatus + "&reason=" + encodeURIComponent(reason) + "&countryOfOrigin=" + countryOfOrigin + "&importedCountry=" + importedCountry + "&fabricContent=" + encodeURIComponent(fabricContent) + "&careInstructions=" + encodeURIComponent(careInstructions) + "&offerDescription=" + encodeURIComponent(offerDescription) + "&onlyAtMacys=" + onlyAtMacys + "&legalOne=" + legalOne + "&legalOneExplain=" + legalOneExplain + "&legalTwo=" + legalTwo + "&legalTwoExplain=" + legalTwoExplain + "&legalThree=" + legalThree + "&legalThreeExplain=" + legalThree + "&legalFour=" + legalFour + "&fifty=" + fifty + //"&createdBy=" + userId "&offerId=" + offerId + "&merchId=" + merchId + "&adID=" + adId + "&eventID=" + eventID + "&adNum=" + adNum var ajaxMaster = new AjaxMaster(sql, "Puma.updateMerchFormForCutData(data)", "", "btiDispatcher.aspx"); sql = "action=updateMerchFormForCut&sql=" + encodeURIComponent(msql); //objAjaxAd.SendQuery(sql); // "[t0].[signedByUserID]," + // "[t0].[signedStatus]," + //"[t0].[dateSigned]," + //"[t0].[signedLastByUserID]," + //"[t0].[dateLastSigned1]," + //"[t0].[signedLastStatus]" + objAjaxAd.main_flag = "updateMerchFormForCutData"; objAjaxAd.SendQuery(sql); //var ajaxMaster = new AjaxMaster(sql, "Puma.updateMerchFormForCutData(data)", "", "puma_core.aspx"); } } }
Write just " and not \" in the onClick attribute, or just omit the quotation marks: onClick=Puma.saveTheCut() The character \ has no special role in HTML; it’s just yet another character. So when you have onClick=\"Puma.saveTheCut()\", the actual attribute value is \"Puma.saveTheCut()\", which does not work of course, as you can see by looking at the console in the Developer Tools of your browser. You should see something the like following there: SyntaxError: illegal character \"Puma.saveTheCut()\" (or with \"yup()\" when testing Agony’s jsfiddle). As it is, the code should not work in any browser, and does not work in my IE 10 either.
Passing parameters to a form, via JavaScript
I have a form which passes a number of parameters. So far, the stock parameters are being passed: var params = "title=" + document.getElementById("title").value + "&url=" + document.getElementById("url").value + "&snippet=" + document.getElementById("snippet").value + "&tags=" + document.getElementById("tags").value + "&status_bookmark=" + document.getElementById("status_bookmark").value + "&comment=" + document.getElementById("comment").value + "&status_comment=" + document.getElementById("status_comment").value; I'm attempting to append additional form elements to this parameter string, which are: var i, lng = document.getElementById('addbookmark').length; // If the length property is undefined, then there is only one checkbox. if (typeof lng === "undefined") { params + "&topic-link-item-1=" + document.getElementById("topic-link-item-1").value; params + "&topic-link-comment-box-1=" + document.getElementById("topic-link-comment-box-1").value; } else { for (i = 0; i < lng; i++) { params + "&topic-link-item-" + i + "=" + document.getElementById("topic-link-item-" + i).value; params + "&topic-link-comment-box-" + i + "=" + document.getElementById("topic-link-comment-box-" + i).value; } } Here, I've used code taken from another StackOverflow article, and as you can see, I'm trying to build up a series of paired parameters that match the ad hoc form elements I'm generating elsewhere via jQuery, which works. However, these values appear not be getting passed via the form, while the other form elements are being passed. Any suggestions? Update I've revised the code, per the suggestions, but it's not working: var i, formObj = document.form['addbookmark'], formObjLng = document.form['addbookmark'].length; // If the length property is undefined, then there is only one checkbox. if ((typeof formObjLng !== "undefined")) { for (i = 0; i < formObjLng; i++) { if ((formObj.elements['topic-link-item-' + i].type == "checkbox") && (formObj.elements['topic-link-item-' + i].checked)) { params = params + "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i)).value; params = params + "&topic-link-comment-box-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-comment-box-" + i)).value; } } } As for the form, it's simply a form with an ID of "addbookmark", and to again state what I said earlier, everything else works with the exception of what Im attempting here.
There are 2 issues with your code. You need to URL encode the values using the encodeURIComponent function. Also you need to assign the result back to the params variable when concatenating: var params = "title=" + encodeURIComponent(document.getElementById("title").value) + "&url=" + encodeURIComponent(document.getElementById("url").value) + "&snippet=" + encodeURIComponent(document.getElementById("snippet").value) + "&tags=" + encodeURIComponent(document.getElementById("tags").value) + "&status_bookmark=" + encodeURIComponent(document.getElementById("status_bookmark").value) + "&comment=" + encodeURIComponent(document.getElementById("comment").value) + "&status_comment=" + encodeURIComponent(document.getElementById("status_comment").value); and also for the other values that you are adding: var i, lng = document.getElementById('addbookmark').length; // If the length property is undefined, then there is only one checkbox. if (typeof lng === "undefined") { params += "&topic-link-item-1=" + encodeURIComponent(document.getElementById("topic-link-item-1").value); params += "&topic-link-comment-box-1=" + encodeURIComponent(document.getElementById("topic-link-comment-box-1").value); } else { for (i = 0; i < lng; i++) { params += "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i).value); params += "&topic-link-comment-box-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-comment-box-" + i).value); } } Notice how: params += "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i).value); which is equivalent to: params = params + "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i).value); is not the same as what you were doing initially: params + "&topic-link-item-" + i + "=" + encodeURIComponent(document.getElementById("topic-link-item-" + i).value); You were simply concatenating 2 values and never assigning the result back to the params variable.
I'm pretty sure Javascript doesnt' allow text append the way you are doing it. Should be params = params +