document.getElementById("toai").innerHTML = document.getElementById("toai").value - javascript

<script>
function toai()
{
var Fr_amt_invt = document.getElementById("Fr_amt_invt").value;
var Re_amt_invt = document.getElementById("Re_amt_invt").value;
var totaltoai = +Fr_amt_invt + +Re_amt_invt;
document.getElementById("toai").innerHTML=+totaltoai;
}
</script>
function toai() result is in .innerHTML i want result in .value format so that it use in form input.

Instead of doing document.getElementById("toai").innerHTML=+totaltoai; in the toai()function you can return the value.
<script>
function toai()
{
var Fr_amt_invt = document.getElementById("Fr_amt_invt").value;
var Re_amt_invt = document.getElementById("Re_amt_invt").value;
Fr_amt_invt = parseFloat(Fr_amt_invt);
Re_amt_invt = parseFloat(Re_amt_invt);
var totaltoai = (Fr_amt_invt + Re_amt_invt);
totaltoai = parseFloat(totaltoai);
//document.getElementById("toai").innerHTML=+totaltoai; // delete this
return totaltoai;
}
Then you can do document.getElementById('some-inputs-id').value = toai();

Related

Google spreadsheet custom function returns nothing

I have a problem debugging the code. It's working if I define values by myself instead of taking it from properties and spreadsheet. I am new to JavaScript so it can be really basic error.
What I am trying to do with this function:
Taking name of person, date and name of school from the spreadsheet
Getting arrays data from the properties that was saved with other function. The saving line in that function looks like this:
PropertiesService.getScriptProperties().setProperty('Mokyklo‌​s', JSON.stringify(Mokyklos));
It's 6 arrays of full names, array of date, array of school names and array of numbers. The numbers array is used to return the answer.
function ApmokMokMokykloje(mokytojas, data, mokykla) {
Utilities.sleep(Math.random() * 1000);
var MokytojaiL = PropertiesService.getScriptProperties().getProperty('MokytojaiL');
var Mokytojai1 = PropertiesService.getScriptProperties().getProperty('Mokytojai1');
var Mokytojai2 = PropertiesService.getScriptProperties().getProperty('Mokytojai2');
Utilities.sleep(Math.random() * 1000);
var Mokytojai3 = PropertiesService.getScriptProperties().getProperty('Mokytojai3');
var Mokytojai4 = PropertiesService.getScriptProperties().getProperty('Mokytojai4');
var Mokytojai5 = PropertiesService.getScriptProperties().getProperty('Mokytojai5');
Utilities.sleep(Math.random() * 1000);
var Datos = PropertiesService.getScriptProperties().getProperty('Datos');
var Mokyklos = PropertiesService.getScriptProperties().getProperty('Mokyklos');
var ApmokMokSkaiciai = PropertiesService.getScriptProperties().getProperty('ApmokMokSkaiciai');
var mokytojaiL = MokytojaiL;
mokytojaiL = JSON.parse(mokytojaiL);
var mokytojai1 = Mokytojai1;
mokytojai1 = JSON.parse(mokytojai1);
var mokytojai2 = Mokytojai2;
mokytojai2 = JSON.parse(mokytojai2);
var mokytojai3 = Mokytojai3;
mokytojai3 = JSON.parse(mokytojai3);
var mokytojai4 = Mokytojai4;
mokytojai4 = JSON.parse(mokytojai4);
var mokytojai5 = Mokytojai5;
mokytojai5 = JSON.parse(mokytojai5);
var datos = Datos;
datos = JSON.parse(datos);
var mokyklos = Mokyklos;
mokyklos = JSON.parse(mokyklos);
var skaicius = ApmokMokSkaiciai;
skaicius = JSON.parse(skaicius);
for(var i = 0; i < mokyklos.length; i++) {
if(data==datos[i] && mokykla==mokyklos[i]) {
if ((mokytojas==mokytojaiL[i]) || (mokytojas==mokytojai1[i]) || (mokytojas==mokytojai2[i]) || (mokytojas==mokytojai3[i]) || (mokytojas==mokytojai4[i]) || (mokytojas==mokytojai5[i])) {
return skaicius[i]; // returns blank
}
}
}
}
The code below works fine.
function testas3(mokytojas, data, mokykla) {
// the same values from spreadsheet
var datapvz="2017-11-04T22:00:00.000Z";
var mokyklapvz="Zalioji mokykla";
var mokytojaspvz="Penivilas Gremlinavičius";
// the same values from properties
var datos = [];
datos[0]="2017-11-08T22:00:00.000Z";
datos[1] = "2017-11-15T22:00:00.000Z";
datos[2] = "2017-11-11T22:00:00.000Z";
datos[3] = "2017-11-03T22:00:00.000Z";
datos[4] = "2017-11-04T22:00:00.000Z";
var mokyklos = [];
mokyklos[0] = "Mokykla nuostabioji";
mokyklos[1] = "Mylimiausioji mokyklele";
mokyklos[2] = "Dar viena mokyykla";
mokyklos[3] = "Raudonoji";
mokyklos[4] = "Zalioji mokykla";
var mokytojaiL = [];
mokytojaiL[0] = "Cristopher Rangel";
mokytojaiL[1] = "Alessandra Knox";
mokytojaiL[2] = "Germtautas Falalavičius";
mokytojaiL[3] = "Lenkgaudė Trikojytė";
mokytojaiL[4] = "Penivilas Gremlinavičius";
var mokytojai1 = [];
mokytojai1[0] = "Mantvydas Špukys";
mokytojai1[1] = "Išeikbaida Visursėkmytė";
mokytojai1[2] = "Svaidgaudė Praperduvienė";
mokytojai1[3] = "Mantvinas Žirgmyla";
mokytojai1[4] = "Mantvinas Žirgmyla";
var mokytojai2 = [];
mokytojai2[0] = "Griovbaida Nepriteklytė";
mokytojai2[1] = "Išeikbaida Visursėkmytė";
mokytojai2[2] = "Griovbaida Nepriteklytė";
mokytojai2[3] = "Arjautauta Fragmentavičiutė";
mokytojai2[4] = "Kastuvaldas Parašiutauskas";
var mokytojai3 = [];
mokytojai3[0] = "Rustautas Celiulionis";
mokytojai3[1] = "Androbauda Parankpapaitė";
mokytojai3[2] = "Arjauvilė Katrakojytė";
mokytojai3[3] = null;
mokytojai3[4] = "Rustautas Celiulionis";
var mokytojai4 = [];
mokytojai4[0] = null;
mokytojai4[1] = null;
mokytojai4[2] = null;
mokytojai4[3] = "Arjauvilė Katrakojytė";
var mokytojai5 = [];
var skaicius = [];
skaicius[0]="99";
skaicius[1]="98";
skaicius[2]="87";
skaicius[3]="89";
skaicius[4]="89";
for(var i = 0; i < mokyklos.length; i++) {
if(datapvz==datos[i] && mokyklapvz==mokyklos[i]) {
if ((mokytojaspvz==mokytojaiL[i]) || (mokytojaspvz==mokytojai1[i]) || (mokytojaspvz==mokytojai2[i]) || (mokytojaspvz==mokytojai3[i]) || (mokytojaspvz==mokytojai4[i]) || (mokytojaspvz==mokytojai5[i]))
{
return skaicius[i]; // returns 89
}
}
}
}
I think the problem is that you're comparing a Date object (from the spreadsheet...e.g., from cell F$1) with a string literal (from your saved JSON object). To make them match, you should call toJSON() on the date object before comparing, like this:
var date = data.toJSON();
for(var i = 0; i < mokyklos.length; i++) {
if(date==datos[i] && mokykla==mokyklos[i]) {
if ((mokytojas==mokytojaiL[i]) || (mokytojas==mokytojai1[i]) || (mokytojas==mokytojai2[i]) || (mokytojas==mokytojai3[i]) || (mokytojas==mokytojai4[i]) || (mokytojas==mokytojai5[i])) {
return skaicius[i];
}
}
}

Undefined global variables in function

I'm trying to use javascript to perform calculations on a button click because I don't want the page to refresh when this button is clicked. I wrote a script below:
var name = document.getElementById('recipeName').value;
var lvl = document.getElementById('recipeLvl').value;
var e = document.getElementById("qualitySelect");
var quality = e.options[e.selectedIndex].value;
e = document.getElementById("classSelect");
var craft = e.options[e.selectedIndex].value;
var mat1 = document.getElementById('material1Name').value;
var mat1Qty = document.getElementById('material1Qty').value;
var mat1NQ = document.getElementById('material1NQ').value;
var mat1NQprice = document.getElementById('material1NQprice').value;
var mat1HQ = document.getElementById('material1HQ').value;
var mat1HQprice = document.getElementById('material1HQprice').value;
var mat2 = document.getElementById('material2Name').value;
var mat2Qty = document.getElementById('material2Qty').value;
var mat2NQ = document.getElementById('material2NQ').value;
var mat2NQprice = document.getElementById('material2NQprice').value;
var mat2HQ = document.getElementById('material2HQ').value;
var mat2HQprice = document.getElementById('material2HQprice').value;
var mat3 = document.getElementById('material3Name').value;
var mat3Qty = document.getElementById('material3Qty').value;
var mat3NQ = document.getElementById('material3NQ').value;
var mat3NQprice = document.getElementById('material3NQprice').value;
var mat3HQ = document.getElementById('material3HQ').value;
var mat3HQprice = document.getElementById('material3HQprice').value;
var mat4 = document.getElementById('material4Name').value;
var mat4Qty = document.getElementById('material4Qty').value;
var mat4NQ = document.getElementById('material4NQ').value;
var mat4NQprice = document.getElementById('material4NQprice').value;
var mat4HQ = document.getElementById('material4HQ').value;
var mat4HQprice = document.getElementById('material4HQprice').value;
var mat5 = document.getElementById('material5Name').value;
var mat5Qty = document.getElementById('material5Qty').value;
var mat5NQ = document.getElementById('material5NQ').value;
var mat5NQprice = document.getElementById('material5NQprice').value;
var mat5HQ = document.getElementById('material5HQ').value;
var mat5HQprice = document.getElementById('material5HQprice').value;
var mat6 = document.getElementById('material6Name').value;
var mat6Qty = document.getElementById('material6Qty').value;
var mat6NQ = document.getElementById('material6NQ').value;
var mat6NQprice = document.getElementById('material6NQprice').value;
var mat6HQ = document.getElementById('material6HQ').value;
var mat6HQprice = document.getElementById('material6HQprice').value;
e = document.getElementById("catalyst1");
var crystal1 = e.options[e.selectedIndex].value;
e = document.getElementById('element1');
var element1 = e.options[e.selectedIndex].value;
var crystal1Qty = document.getElementById('crystalQty1').value;
var crystal1Price = document.getElementById('crystalPrice1').value;
e = document.getElementById("catalyst2");
var crystal2 = e.options[e.selectedIndex].value;
e = document.getElementById('element2');
var element2 = e.options[e.selectedIndex].value;
var crystal2Qty = document.getElementById('crystalQty2').value;
var crystal2Price = document.getElementById('crystalPrice2').value;
var notes = document.getElementById('notes').value;
var marketPrice = document.getElementById('marketPrice').value;
function calculate() {
var mat1Cost = (mat1NQ * mat1NQprice) + (mat1HQ * mat1HQprice);
var mat2Cost = (mat2NQ * mat2NQprice) + (mat2HQ * mat2HQprice);
var mat3Cost = (mat3NQ * mat3NQprice) + (mat3HQ * mat3HQprice);
var mat4Cost = (mat4NQ * mat4NQprice) + (mat4HQ * mat4HQprice);
var mat5Cost = (mat5NQ * mat5NQprice) + (mat5HQ * mat5HQprice);
var mat6Cost = (mat6NQ * mat6NQprice) + (mat6HQ * mat6HQprice);
var crystal1Cost = (crystal1Qty * crystal1Price);
var crystal2Cost = (crystal2Qty * crystal2Price);
var total = mat1Cost +
mat1Cost +
mat1Cost +
mat1Cost +
mat1Cost +
mat1Cost +
crystal1Cost +
crystal2Cost;
document.getElementById('totalCost').value = mat1Cost;
return false;
}
I'm new to javascript, and thought declaring the variables right after would make them accessible to any function that I would need to use them in, but they are undefined in the calculate function. If I declare them in calculate() it's fine, so is this a scope problem?
Thanks in advance for any help!
The problem is you're grabbing the elements values before you've assigned anything to them. You're getting a copy, not a reference.
var initialValue = document.getElementById('text').value;
function clickHandler() {
// Notice how I have to grab the value again when I want an updated value
var updatedValue = document.getElementById('text').value;
console.log('Initial:', initialValue);
console.log('Updated:', updatedValue);
}
<input id="text" />
<button onclick="clickHandler()">Click Me</button>
#epascarello has commented the real answer, there is no much science there, this is javascript, therefore is a script language, so as soon as this file is loaded, those variables are filled with the initial data of each field, so, if you want to get the value at the point you press calculate(), you should get your variables.

Reading and writing values from form doesn't work

I have a code sample where I want to read some data from input fields and write that data to a different div.
In first section the code works properly, but in 2nd section the value is not shown. I used the alert function to check that wether value is received or not. The value is received but not shown.
What is wrong with my code? (below)
function preview() {
$(".preview_block").fadeIn(1000);
$("body").css("overflow","hidden");
$(".preview_block").css("overflow","auto");
/*======================Value fetch for personal details=======================*/
var fastname1 = document.forms["myform"]["fastname"].value;
if(fastname1==""){fastname1="---null---"}
var lastname1 = document.forms["myform"]["lastname"].value;
if(lastname1==""){lastname1="---null---"}
var sex1 = document.forms["myform"]["radio"].value;
if(sex1==""){sex1="---null---"}
var clgid1 = document.forms["myform"]["clgid"].value;
if(clgid1==""){clgid1="---null---"}
var dob1 = document.forms["myform"]["dob"].value;
if(dob1==""){dob1="---null---"}
var fname1 = document.forms["myform"]["fname"].value;
if(fname1==""){fname1="---null---"}
var mname1 = document.forms["myform"]["mname"].value;
if(mname1==""){mname1="---null---"}
var gname1 = document.forms["myform"]["gname"].value;
if(gname1==""){gname1="---null---"}
var mobno1 = document.forms["myform"]["mobno"].value;
if(mobno1==""){mobno1="---null---"}
var pmobno1 = document.forms["myform"]["pmobno"].value;
if(pmobno1==""){pmobno1="---null---"}
var mail1 = document.forms["myform"]["mail"].value;
if(mail1==""){mail1="---null---"}
var addr11 = document.forms["myform"]["addr1"].value;
if(addr11==""){addr11="---null---"}
var addr21 = document.forms["myform"]["addr2"].value;
if(addr21==""){addr21="---null---"}
var city1 = document.forms["myform"]["city"].value;
if(city1==""){city1="---null---"}
var state1 = document.forms["myform"]["state"].value;
if(state1==""){state1="---null---"}
var pcode1 = document.forms["myform"]["pcode"].value;
if(pcode1==""){pcode1="---null---"}
var ps1 = document.forms["myform"]["ps"].value;
if(ps1==""){ps1="---null---"}
var po1 = document.forms["myform"]["po"].value;
if(po1==""){po1="---null---"}
var country1 = document.forms["myform"]["country"].value;
if(country1==""){country1="---null---"}
document.getElementById("p1").innerHTML = fastname1;
document.getElementById("p2").innerHTML = lastname1;
document.getElementById("p3").innerHTML = sex1;
document.getElementById("p4").innerHTML = clgid1;
document.getElementById("p5").innerHTML = dob1;
document.getElementById("p6").innerHTML = fname1;
document.getElementById("p7").innerHTML = mname1;
document.getElementById("p8").innerHTML = gname1;
document.getElementById("p9").innerHTML = mobno1;
document.getElementById("p10").innerHTML = pmobno1;
document.getElementById("p11").innerHTML = mail1;
document.getElementById("p12").innerHTML = addr11;
document.getElementById("p13").innerHTML = addr21;
document.getElementById("p14").innerHTML = city1;
document.getElementById("p15").innerHTML = state1;
document.getElementById("p16").innerHTML = pcode1;
document.getElementById("p17").innerHTML = ps1;
document.getElementById("p18").innerHTML = po1;
document.getElementById("p19").innerHTML = country1;
/*======================Value fetch for XII standered details=======================*/
var qualification1 = $('#qualification option:selected').html();
if(qualification1==""){qualification1="---null---"}
var q1_board1 = $('#q1_board option:selected').html();
if(q1_board1==""){q1_board1="---null---"}
var clg_nm1 = document.forms["myform"]["clg_nm"].value;
if(session1==""){session1="---null---"}
var regno1 = document.forms["myform"]["regno"].value;
if(regno1==""){regno1="---null---"}
var yr_reg1 = document.forms["myform"]["yr_reg"].value;
if(yr_reg1==""){yr_reg1="---null---"}
var rollno1 = document.forms["myform"]["rollno"].value;
if(rollno1==""){rollno1="---null---"}
document.getElementById("q1").innerHTML = qualification1;
document.getElementById("q2").innerHTML = q1_board1;
document.getElementById("q3").innerHTML = clg_nm1;
document.getElementById("q4").innerHTML = regno1;
document.getElementById("q5").innerHTML = yr_reg1;
document.getElementById("q6").innerHTML = rollno1;
}

Javascript array to point variable name with similar function

I'm want to shorten this function with array
document.mySchedule.breakfast.onchange = function() {
var id = document.mySchedule.breakfast.selectedIndex;
var val = document.mySchedule.breakfast[id].value;
strUser[0]=val;
}
document.mySchedule.lunch.onchange = function() {
var id = document.mySchedule.lunch.selectedIndex;
var val = document.mySchedule.lunch[id].value;
strUser[1]=val;
}
document.mySchedule.dinner.onchange = function() {
var id = document.mySchedule.dinner.selectedIndex;
var val = document.mySchedule.dinner[id].value;
strUser[2]=val;
}
I'm try this way but it didn't seem to be work.
var selectData = ["breakfast","lunch","dinner"]
for(i = 0; i < selectData.lenght; i++) {
document.mySchedule.selectData[i].onchange = function() {
var id = document.mySchedule.selectData[i].selectedIndex;
var val = document.mySchedule.selectData[i][id].value;
strUser[i]=val;
}
}
by the way this use to control selector.
Hope someone can help. thank you
Use:
var selectData = ["breakfast","lunch","dinner"]
for(i = 0; i < selectData.lenght; i++) {
var e = document.getElementById(selectData[i]);
e.onchange = function(){
strUser[i] = e.value;
};
}

Javascript & JQuery odd issue or what did I do wrong?

I have this function for coping values from window to window that is working one way, but not another...
Working script:
$(document).ready(function(e) {
$('#clickit').live({
click: function() {
window.opener.document.forms['orderForm']['service'].value = document.forms['GroundRates']['service'].value;
window.opener.document.forms['orderForm']['rate'].value = document.forms['GroundRates']['rate'].value;
self.close();
return false;
}
});
});
Now I on this other script, what did I do wrong? I'm pulling my hair out here.
Not working:
$(document).ready(function(e) {
$('#clickit').live({
click: function() {
var thisservice = document.forms['GroundRates']['service'].value;
var thisrate = document.forms['GroundRates']['rate'].value;
var thatservice = window.opener.document.forms['orderForm']['service'].value;
var thatrate = window.opener.document.forms['orderForm']['rate'].value;
$(thatrate) = $(thisrate);
$(thatservice) = $(thisservice);
self.close();
return false;
}
});
});
I've also tried..
$(thatrate).val() = $(thisrate).val();
$(thatservice).val() = $(thisservice).val();
And..
thatrate = thisrate;
thatservice = thisservice;
But this works:
var service = document.forms['GroundRates']['service'].value;
var rate = document.forms['GroundRates']['rate'].value;
window.opener.document.forms['orderForm']['service'].value = service;
window.opener.document.forms['orderForm']['rate'].value = rate;
Am I not assigning the var correctly for the window.opener?
You're misusing .val()
$(thatrate).val($(thisrate).val());
$(thatservice).val($(thisservice).val());
The new value goes inside the parentheses.
var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];
thatrate.value = thatservice.value;
thatservice.value = thisservice.value;
or if you want to wrap the DOM objects with a jQuery object.
var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];
$(thatrate).val( $(thatservice).val() );
$(thatservice).val( $(thisservice).val() );
try:
var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];
thatrate.val(thisrate.val());
thatservice.val(thisservice.val());
Your console will tell you: ReferenceError: Invalid left-hand side in assignment
$(thatrate) = $(thisrate);
$(thatservice) = $(thisservice);
You should do it like this:
var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];
thatrate.value = thisrate.value
thatservice.value = thisservice.value;

Categories