Split cookies from page [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
I was doing some coding today, but I got an error:
Cannot read properties of undefined (reading 'split')
at getCookie ((index):38:49)
at (index):47:31
My code (begins at line 36, ends at 43):
var cookieArray = document.cookie.split(";");
for (var i = 0; i < cookieArray.length; i++) {
var cookiePair = cookieArray[1].split("=");
if(name == cookiePair[0].trim()) {
return decodeURIComponent(cookiePair[1])
}
}
Btw, I've read that you can only split a string, but this is a string right?

You should put i instead of 1:
for (var i = 0; i < cookieArray.length; i++) {
var cookiePair = cookieArray[i].split("=");
if(name== cookiePair[0].trim()) {
return decodeURIComponent(cookiePair[i])
}
}
Because the first time it iterates the array cookieArray[1] my be undefind.

Related

JS - Expected a conditional expression and instead saw an assignment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
The piece of code below seems to work in online code editors but when using on Cloud9 IDE, it comes up with that error message. Is there a way to sort this or alternatively write this bit of code?
The error appears on line 3, the for loop statement.
var text1 = document.querySelector('input[name="contract-type"]').value;
var select1 = document.getElementById('contract-type-list');
for(var i, j=0; i = select1.options[j]; j++){
if(i.text == text1){
select1.selectedIndex = j;
break;
}
}
The linter is warning you that i = select1.options[j] is a strange expression to be checking for truthyness, because it's an assignment. While you could ignore the rule, a better approach would be to iterate through the option children from querySelectorAll or .children or with the collection's iterator instead of going through .options[index].
var text1 = document.querySelector('input[name="contract-type"]').value;
var select1 = document.getElementById('contract-type-list');
for (const [i, option] of [...select1.options].entries()) {
if (option.text == text1) {
select1.selectedIndex = i;
break;
}
}
Or, if the value is definitely one of the options, just do
document.getElementById('contract-type-list').value = document.querySelector('input[name="contract-type"]').value;
or, if it might not exist:
const inputText = document.querySelector('input[name="contract-type"]').value;
const select = document.getElementById('contract-type-list');
const matchingOption = [...select.children].find(option => option.text === inputText);
if (matchingOption) {
select.value = matchingOption.ariaValueMax;
}

I am trying to concatenate the second parameter to the first in JS [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm pretty stuck, It should evaluate to ["present", "pretty", "precede"]
function addTooStart(x, y) {
let a = [];
let b = "";
for (let i in x) {
a = x.push(y);
a = x[b]
}
return a
}
let abc = addTooStart(["sent", "tty", "cede"], "pre");
console.log(abc)
I'm not sure what your a and b are attempting to do here. Some problems from your code:
x.push(y) adds the element y to the end of the array x, and then returns the new length of the array, so now a is a number.
x[b] will always be an invalid call, since b equals the empty string and is never changed, arrays are integer indexed.
The general approach here would be to loop through the array x, like you did, then for each element, set it equal to "y + current element". I have attached a working version below.
function addToStart(x,y){
for (let i = 0; i < x.length; i++) {
x[i] = y + x[i]
}
return x
}
addToStart([ "sent", "tty", "cede" ], "pre"); // -> [ 'present', 'pretty', 'precede' ]

Filter a table by Javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
<script type="text/javascript">
function filterResults() {
var trTag = document.getElementsByTagName("tr");
for (var i = 0; i < trTag.length; i++) {
if (trTag[i].OuterHTML.includes(filterTXT.Value)) {
trTag.Style.Display = "none";
}
}
}
</script>
Firefox tells me: trTag[i].OuterHTML is undefined. I assume this is because OuterHTML does not exist? If so, what do I use instead?
JavaScript is a case-sensitive language.
JavaScript is case sensitive. It is common to start the name of a constructor with a capitalised letter, and the name of a function or variable with a lower-case letter (ref).
You might want to make these changes
outerHTML not OuterHTML (ref)
style not Style (ref)
display not Display (ref)
value not Value (ref)
in your script.
<script type="text/javascript">
function filterResults() {
var trTag = document.getElementsByTagName("tr");
for (var i = 0; i < trTag.length; i++) {
if (trTag[i].outerHTML.includes(filterTXT.value)) {
trTag.style.display = "none";
}
}
}
</script>

Missing ) after arguement list. (line 6 file "code") [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm new to coding and a got this pre set code to use within a spreadsheet for a google awesome table, and I keep getting this error.
Missing ) after argument list. (line 6, file "Code)
Here is my code:
var formURL = 'https://docs.google.com/a/waitrose.co.uk/forms/d/11-z44oW1ixP1tShkwjrBpa0DptOA2IinU5MCCkUvf0o/viewform';
var sheetName = 'Form responses 1';
var columnIndex = 5;
function getEditResponseUrls(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Form responses 1);
var data = sheet.getDataRange().getValues();
var form = FormApp.openByUrl(formURL);
for(var i = 2; i < data.length; i++) {
if(data[i][0] != '' && (data[i][columnIndex-1] == '' || !data[i][columnIndex-1])) {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if(formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex).setValue('<div style="width:100px">Edit entry</div>');
}
}
}
Looks like you are missing a pair of quotes on line 6:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Form responses 1);
should be
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form responses 1");

Javascript - Passing from a DOM element to array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Hello and thank you for your time.
Here is the code :
<script>
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0, c = names.length ; i < c ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);// the results are : undefined
}
</script>`
I've tried to use the method tostring, or to push the results into the array but without success.
Thanks
Your main issue seems to be fixed. Make sure the DOM has been loaded before you try to run your code, and there is no need for two variables in your loop. Simplify it like below:
window.onload = function () {
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0 ; i < names.length ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);
}
};
Fiddle
ar.length equals 0, because you just declare the array, but dont put anything into it. I think what you wanted to do is the following:
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0 ; i < names.length ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);
}

Categories

Resources