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;
};
}
Related
I want to do a comparate a elementes inside of an array for get a specific value, how i could do it with this?
var data = sheet.getDataRange().getValues();
var dataToImport = {};
for(var i = 1; i < data.length; i++) {
dataToImport = {
Nivel:data[i][0],
Consumo:data[i][1],
Flujo:data[i][2],
if(dataToImport[Nivel]>99)
{
}
I'm not sure what your looking for but hopefully this will help.
function myfunction()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var data=sh.getDataRange().getValues();
var dataToImport = {};
for(var i=1;i<data.length;i++)
{
dataToImport = {'Nivel':data[i][0],'Consumo':data[i][1],'Flujo':data[i][2]}
if(dataToImport['Nivel']>99)
{
Logger.log('Nivel greater than 99 dataToImport = %s', dataToImport);
}
Logger.log(dataToImport);
}
}
Below is my code. if we use set timeout function we got all data but we not use we are not getting inspection data which is coming from my local db. i want use deferred and promises in my code. Thanks in advance.
function onclickToCall() {
this.$('.check-list > .check-list-box').each(function (i) {
var j = i + 1;
var answer_req = $(this).attr("data-isReq");
var checklist_guid = $(this).attr("data-guid");
var question_type = $(this).attr("data-type");
var yes_no = $("input:radio[name='radio_" + j + "']:checked").val();
var notes = $(this).find('#txtAreaNotes_' + j).val();
var attachment_url = $(this).find(".txtCameraImage").attr("data-path");
var appConfig = DriverConnectApp.State.get('config_settings');
var item = {};
item.checklist_guid = checklist_guid;
item.yes_no = yes_no;
item.attachment_url = attachment_url;
item.notes = notes;
if (question_type == 2) { // For Vehical visual inspection
var dataPromise = that.getInspectionData(checklist_guid);
dataPromise.done(function (response, vh_image) {
var inspectionItem = {};
inspectionItem.vh_url = vh_image;
inspectionItem.details = response;
item.vh_inspection = inspectionItem;
that.detailsArr.push(item);
});
} else {
item.vh_inspection = {};
that.detailsArr.push(item);
}
});
// after finish and push all data we need to call function here
test();
}
getInspectionData: function (checklist_guid) {
var that = this;
var deferred = $.Deferred();
that.dbchecklist.getVehicleInspectionlist(checklist_guid, function (data, res) {
var details = [];
if (data.length) {
var img = data[0].vh_image;
var arr = JSON.parse(data[0].vh_details);
for (var k = 0; k < arr.length; k++) {
var items = {};
items.number = arr[k].number;
items.notes = arr[k].notes;
items.attachment_url = arr[k].attachment_url;
details.push(items);
}
deferred.resolve(details, img);
} else {
deferred.resolve(details, img);
}
});
return deferred.promise();
}
In the following code i am trying to populate the javascript class,such that i get an object which i can stringify to json string.
function classValue(valuearrs) {
for (var i = 0; i < valuearrs.length; i++) {
for (j = 0; j < valuearrs[i].length; j++) {
this.id = valuearrs[i][j];
this.name = valuearrs[i][j];
this.somekey = valuearrs[i][j];
}
}
}
function CreatenestedarrObj( valuearr) {
this.Arrayparameters = [new classValue (valuearr)];
}
function ArrayMethodCall() {
var valuearr = new Array();
valuearr[0] = "myval1";
valuearr[1] = "myval2";
valuearr[2] = "myval3";
var valuearr1 = new Array();
valuearr1[0] = "myval11";
valuearr1[1] = "myval12";
valuearr1[2] = "myval13";
nestedarr = new Array();
nestedarr[0] = valuearr;
nestedarr[1] = valuearr1;
var x = new CreatenestedarrObj( nestedarr);
var strobject = JSON.stringify(x);
alert(strobject);
}
</script>
</head> <body> MethodCall: <input type="button" value="Call Method" onclick=" ArrayMethodCall ()" />
After Stringifying the value expected is {ArrayParameters:[{myval1,myval2,myval3},{myval1,myval12,myval13}]}.The thing i can think i am missing is creating new object each time in the loop but how ? or i might be totally wrong.Any help will be much appreciated.
try this
function classValue(valuearrs) {
var arr = []
for (var i = 0; i < valuearrs.length; i++) {
for (j = 0; j < valuearrs[i].length; j++) {
var temp = new Object();
temp.id = valuearrs[i][j];
temp.name = valuearrs[i][j];
temp.somekey = valuearrs[i][j];
arr[j] = temp;
}
}
return arr;
}
Change this:
function classValue(valuearrs) {
var arr=[]
for (var i = 0; i < valuearrs.length; i++) {
arr[i]={};
for (j = 0; j < valuearrs[i].length; j++) {
arr[i].id = valuearrs[i][j];
arr[i].name = valuearrs[i][j];
arr[i].somekey = valuearrs[i][j];
}
}
return arr;
}
i don't know why you need to split the nestedarr to id,name and somekey
but if you don't need to, just return the valuearrs and you will be fine:
function classValue(valuearrs) {
return valuearrs;
}
function CreatenestedarrObj( valuearr) {
this.Arrayparameters = [new classValue (valuearr)];
}
function ArrayMethodCall() {
var valuearr = new Array();
valuearr[0] = "myval1";
valuearr[1] = "myval2";
valuearr[2] = "myval3";
var valuearr1 = new Array();
valuearr1[0] = "myval11";
valuearr1[1] = "myval12";
valuearr1[2] = "myval13";
nestedarr = new Array();
nestedarr[0] = valuearr;
nestedarr[1] = valuearr1;
var x = new CreatenestedarrObj( nestedarr);
var strobject = JSON.stringify(x);
console.info(strobject);
}
the output is
{"Arrayparameters":[[["myval1","myval2","myval3"],["myval11","myval12","myval13"]]]}
or far better if you don't want it as Arrayparameters, you can use following code
var strobject = JSON.stringify(nestedarr);
and the output will be like this :
[["myval1","myval2","myval3"],["myval11","myval12","myval13"]]
var select = [];
for (var i = 0; i < nameslots; i += 1) {
select[i] = this.value;
}
This is an extract of my code. I want to generate a list of variables (select1, select2, etc. depending on the length of nameslots in the for.
This doesn't seem to be working. How can I achieve this? If you require the full code I can post it.
EDIT: full code for this specific function.
//name and time slots
function gennametime() {
document.getElementById('slots').innerHTML = '';
var namelist = editnamebox.children, slotnameHtml = '', optionlist;
nameslots = document.getElementById('setpresentslots').value;
for (var f = 0; f < namelist.length; f += 1) {
slotnameHtml += '<option>'
+ namelist[f].children[0].value
+ '</option>';
};
var select = [];
for (var i = 0; i < nameslots; i += 1) {
var slotname = document.createElement('select'),
slottime = document.createElement('select'),
slotlist = document.createElement('li');
slotname.id = 'personname' + i;
slottime.id = 'persontime' + i;
slottime.className = 'persontime';
slotname.innerHTML = slotnameHtml;
slottime.innerHTML = '<optgroup><option value="1">00:01</option><option value="2">00:02</option><option value="3">00:03</option><option value="4">00:04</option><option value="5">00:05</option><option value="6">00:06</option><option value="7">00:07</option><option value="8">00:08</option><option value="9">00:09</option><option value="10">00:10</option><option value="15">00:15</option><option value="20">00:20</option><option value="25">00:25</option><option value="30">00:30</option><option value="35">00:35</option><option value="40">00:40</option><option value="45">00:45</option><option value="50">00:50</option><option value="55">00:55</option><option value="60">1:00</option><option value="75">1:15</option><option value="90">1:30</option><option value="105">1:45</option><option value="120">2:00</option></optgroup>';
slotlist.appendChild(slotname);
slotlist.appendChild(slottime);
document.getElementById('slots').appendChild(slotlist);
(function (slottime) {
slottime.addEventListener("change", function () {
select[i] = this.value;
});
})(slottime);
}
}
You'll have to close in the iterator as well in that IIFE
(function (slottime, j) {
slottime.addEventListener("change", function () {
select[j] = this.value;
});
})(slottime, i);
and it's only updated when the element actually change
The cool thing about JavaScript arrays is that you can add things to them after the fact.
var select = [];
for(var i = 0; i < nameSlots; i++) {
var newValue = this.value;
// Push appends the new value to the end of the array.
select.push(newValue);
}
I have this for loop that gets the id and text of parents elements:
for (var i = 1; i < parents_num; i++)
{
var prev_parent_text = jQuery(id).parent().parent().find('> .myclass').text();
var prev_parent_id = jQuery(id).parent('ul').parent('li').attr('id');
}
What I am trying to do is to increase the number of parent() by 2 in each loop:
For example,
for i = 1:
var prev_parent_text = jQuery(id).parent().parent().find('> .myclass').text();
var prev_parent_id = jQuery(id).parent('ul').parent('li').attr('id');
for i = 2:
var prev_parent_text = jQuery(id).parent().parent().parent().parent().find('> .myclass').text();
var prev_parent_id = jQuery(id).parent('ul').parent('li').parent('ul').parent('li').attr('id');
for i = 3:
var prev_parent_text = jQuery(id).parent().parent().parent().parent().parent().parent().find('> .myclass').text();
var prev_parent_id = jQuery(id).parent('ul').parent('li').parent('ul').parent('li').parent('ul').parent('li').attr('id');
and so on..
I have used the eq() function unsuccesfully:
var num = (i*2) - 2
var prev_parent_text = jQuery(id).parent().eq(num).find('> .myclass').text();
var prev_parent_id = jQuery(id).parent('ul').parent('li').eq(num).attr('id');
Thank you for any help
Keep a reference for each, and update the reference for each step:
var $id = jQuery(id);
var $li = jQuery(id);
for (var i = 1; i < parents_num; i++) {
$id = $id.parent().parent();
$li = $li.parent('ul').parent('li');
var prev_parent_text = $id.find('> .myclass').text();
var prev_parent_id = $li.attr('id');
}
You could add a custom jquery function for that, like this:
$.fn.nparent = function(n) {
var elem = $(this);
for (var i=0;i<n;i++) {
elem = elem.parent();
}
return elem;
}
And use it like:
var id = $('#element').nparents(4).attr('id');