set max input using looping compact in blade script laravel - javascript

I want to set max number from selected item. data max number from controller compact.
after i use select2 and datatables its cannot working. maybe I accidentally deleted an asset or something.
function max() {
var item_selected = document.getElementById("select").value;
var item= {!! json_encode($item->toArray(), JSON_HEX_TAG) !!};
//when i see the source
//var item= [{"id":1,"owned":1},{"id":2,"owned":10},{"id":3,"owned":12}];
for (i = 0; i<=item.length; i++){
//when i use console.log
//it still show id but the error moves to this with same error.
//console.log(item[i].id)
if (item[i].id==item_selected)
{
var input = document.getElementById("number");
input.setAttribute("max",item[i].owned);
}
}
}
the error part is if (item[i].id==item_selected)
and it said Uncaught TypeError: Cannot read property 'id' of undefined

for (i = 0; i<=owned.length; i++)
this line should be
for (i = 0; i<=item.length; i++)
side note: you can use a localized variable to avoid inlining blade within your javascript

Related

Issue with object property '0' of undefined

My code yields the following error:
Uncaught TypeError: Cannot set property '0' of undefined
First, here's the screenshot of the table
Note:
This table are the assigned work schedule of the student.
Let's proceed to my code:
function saveWorkSched(){
// listWorkSched
var arr=[];
var getSAWorkSched=[[],[]];
var wsCounter=0;
var wsCounter2=0;
var j = 0;
$("#listWorkSched td").each(function(){
arr.push($(this).text());
});
console.log(arr);
for(j;j<arr.length;j++){
if(wsCounter2<=2){
getSAWorkSched[wsCounter][wsCounter2]=arr[j];
wsCounter2++;
}else{
wsCounter++;
wsCounter2=0;
getSAWorkSched[wsCounter][wsCounter2]=arr[j];
wsCounter2++;
}
}
}
1st phase:
after the user create the work schedule this will be stored in arr variable.
2nd phase:
the arr value will converted to multi-dimensional array and will be stored in getSAWorkSched variable
after the 3rd loop an error will occurred. it means that every time I create a work schedule more than 2 the error will trigger.
else{
wsCounter++;
wsCounter2=0;
getSAWorkSched[wsCounter][wsCounter2]=arr[j]; // Here's the code where the error specified based on the console of my browser
wsCounter2++;
}
You need to define the nested array that you try to access. It really comes down to the same principle: you address the following:
getSAWorkSched[wsCounter][wsCounter2]
... with a wsCounter value that is eventually getting to 2, but you have only defined two nested arrays in the initialisation of getSAWorkSched, so getSAWorkSched[2] does not exist -- it will give you undefined. Trying to get an array element from nothing (undefined) is not possible. So add this line before it in the else bock:
getSAWorkSched[wsCounter] = []; // <--- Add this
getSAWorkSched[wsCounter][wsCounter2]=arr[j];
More elegant code
You could use $.map and slice to write this in a more elegant way:
function saveWorkSched() {
var arr = $.map($("#listWorkSched td"), function (td) {
return $(td).text();
});
var getSAWorkSched = [];
for (var j = 0; j < arr.length; j += 3) {
getSAWorkSched.push(arr.slice(j, j + 3));
}
}

Javascript json array values

I have the following JSON:
{"title":"MYTITLE","employer":"MYEMPLOYER","weekends":[["6"],["8"],["15"]]}
I need to access weekends to get this:
6
8
15
Here is my try:
var json = $.parseJSON(data);
for (var i = 0, len = data.weekends.length; i < len; i++) {
console.log(data.weekends[i]);
}
But results are empty in chrome console log...if I understand correctly...i read length of json converted to an array and for in loop I read the value in the index array.
But I always get the error:
Uncaught TypeError: Cannot read property 'length' of undefined
Why is weekends length undefined? I set command length and it does not recognize it is an array and to count length so that for loop can work.
The problem is that you are accessing weekend using data, which is just raw JSON String. You need to access the json variable, which is the parsed JSON.
Below is the working code:
const data = {"title":"MYTITLE","employer":"MYEMPLOYER","weekends":[["6"],["8"],["15"]]};
// To avoid calculation of length on every iteration
const weekendLength = data.weekends.length;
for(let i = 0; i < weekendLength; i++){
console.log(data.weekends[i][0]);
}
Here, I am setting the JSON data directly to a variable. In your case, you are most probably getting the data from a network request. In that case, as you did in your code, you need to parse it first. (By calling $.parseJSON())
var data = {"title":"MYTITLE","employer":"MYEMPLOYER","weekends":[["6"],["8"],["15"]]};
for (var i = 0, len = data.weekends.length; i < len; i++){
console.log(data.weekends[i]);
}

Submitting a form through google scripts

I need to submit a form in a google script but get this error:
TypeError: Cannot call method "withItemResponse" of undefined
According to the link below, this is how it should be set up https://developers.google.com/apps-script/reference/forms/form#createResponse()
Code:
//Submit form
var formID = row[24];
var form = FormApp.openById(formID);
Logger.log(form.getId()); //returns correct ID
form.createResponse() ;
form.FormResponse.withItemResponse('Core Teachers', logSummary);
//form has only two questions, a short text and a paragraph text
form.FormResponse.submit();
form.createResponse() returns a FormResponse, which you need to assign to a variable.
also, withItemResponse() expects an object of type ItemResponse. I am not familiar with google forms, but maybe this gets you in the right direction:
var formID = row[24];
var form = FormApp.openById(formID);
var formResponse = form.createResponse();
// get items of form and loop through
var items = form.getItems();
for (index = 0; index < a.length; ++index) {
var item = items[index]
// Cast the generic item to the text-item class. You will likely have to adjust this part. You can find the item classes in the documentation. https://developers.google.com/apps-script/reference/forms/item-type.
if (item.getType() == 'TEXT') {
var textItem = item.asTextItem();
var itemresponse = textItem.createResponse('Core Teachers');
formResponse.withItemResponse(itemresponse);
}
}
formResponse.submit();
Generally, when the documentation of a method lists as parameter type something else than primitive types like String or Boolean you need to create or aquire an object of that type, like I did with createResponse. You need to familiarize yourself with these and other principles because the GoogleAppsScript documentation assumes knowledge of them.

collect data by id using javascript

I have to save temporary data for my webpage using java script.This is the way that i save i one by one since the data is an array.
var product= new Array();
product[1] = document.getElementById("product[1]").value;
product[2] = document.getElementById("product[2]").value;
This method is working. but when i run it by looping, it doesnt work.
for(var i=1; i < no_item; i++){
product[i] = document.getElementById("product[i]").value;
}
*product[] is a varibale that I take from a html dropdown menu
Can anyone please tell me the problem ? thanks ~ =)
Should be written as, as you are going to be getting the id "product[i]" every time with your original code. This will get "product[1]" then "product[2]" and so on:
for(var i=1; i < no_item; i++){
product.push(document.getElementById("product[" + i + "]").value);
}
Also, as a comment, we tend to prefer var product = []; over var product = new Array(); in javascript but both will work.

got stuck with this set of code in jquery validate

var formRules = $(this).data('rules');
var formValues = $(this).data('values');
if(formRules || formValues){
var rulesArray = formRules.split(',');
var valuesArray = formValues.split(',');
for(var i=0; i < rulesArray.length; i++){
//alert(rulesArray[i]);
$.validationEngine.defaults.rulesArray[i] = valuesArray[i];
}
}
else{
return false;
}
This throws an error like following
Error: TypeError: $.validationEngine.defaults.rulesArray is undefined
Source File: http://localhost:8380/javascript/jquery.validationEngine.js
Line: 2092
I cannot find the problem with this code.Any help is welcome
EDIT:
I am trying to set the global options eg:scroll using the for loop.
The formRules string will have these options comma seperated and the corresponding values in the formValues string.
So i am expecting it to come like $.validationEngine.defaults.scroll = true;
change this line
$.validationEngine.defaults.rulesArray[i] = valuesArray[i];
to this
$.validationEngine.defaults[rulesArray[i]] = valuesArray[i];
rulesArray is not a child of $.validationEngine.defaults. The values stored in your rulesArray are. The syntax in my second code block references everything properly.
This is called Bracket Notation, a way to get an object's property using any sort of valid calculation (like rulesArray[i], or "myStringPropertyName"). see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators for other methods.

Categories