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 5 years ago.
Improve this question
I want to change all instances of the word "cão" on a page to read "gato" (following an older tutorial).
All instances of "cão" are inside a span and I´m trying to change them by using getElementsByTagName method.
Since there more than one, I cant use innerHTML to change the content so I´m using a for loop to cycle through all positions but I´m getting a sytax error after the increment i++. Why is that?
var elementoHeading = document.getElementById('heading');
elementoHeading.innerHTML = "Tudo sobre gatos";
var nomesTags = document.getElementsByTagName("span");
for (var i = 0, i < nomesTags.length, i++) {
nomesTags[i].innerHTML = "gato";
}
Use semicolons, not commas, in the for construct:
for (var i = 0; i < nomesTags.length; i++) {
^ ^
The reason the syntax error is after the increment is because the JS engine expects 3 statements inside of the for's brackets, but you only gave one (commas don't terminate the statement).
Related
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 years ago.
Improve this question
I'm trying to use a for loop to display all the elements in my array that are greater than 5. I am using an if-statement to determine the values greater than five, but when I run the code, I get values printed to the console that are both below and over five. I have tried creating variables to store the index values and tried using the && operator, but none of these have worked.
Here is the code for reference:
var myArray = [];
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
console.log("Original: " + myArray);
console.log("Values greater than 5");
// the part I have issues with
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] > 5) {
console.log(i);
}
}
You are printing i not the array element.
try
console.log(myArray[i]);
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 4 years ago.
Improve this question
I'm trying to take a string and check whether or not it contains a lowercase letter or number, and then if so push that letter or number to an array.
for(let i = 0; i < datearg.length; i++)
{
log.info(datearg.charAt(i));
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
if(/[0-9]/.test(datearg.charAt(i))) number_num++; numbers.push(datearg.charAt(i));
}
However, both if statements always evaluate to true and the arrays end up containing every single character in datearg. Anyone know why?
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
is equivalent to
if(/[a-z]/.test(datearg.charAt(i))) { letter_num++; }
letters.push(datearg.charAt(i));
i.e. push is not conditional. This is the primary reason why many style guides heavily discourage control structures without braces (which only take a single statement).
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 6 years ago.
Improve this question
I'm creating a site and i needed to do some js. I'm not that good with it but tought i would figer it out. Not. I created a for loop but it does not run.
function order(user,product){
var index;
for(var i = 0; i<users.lenght; i++){
if(user == users[i]){
index = i;
break;
}
}
var budget = budgets[index];
alert(budget);
}
the creation of the users and budgets arrays are done with php and after checking with alert() it was how it should be.
Can anyone help me please?
lenght is spelt length. The misspelt property does not exist, so it undefined, which is equivalent to 0.
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
I am using some simple javascript as below, but for some reasn the catParam is failing with error missing : after id. please help.
var catParam = "(id=cat00000)";
var inputParams = {serviceID:"getCategories",apiKey="asdfasfgx6",catCriterior:catParam};
Use
var catParam = "(id=cat00000)";
var inputParams = {serviceID:"getCategories",apiKey : "asdfasfgx6",catCriterior:catParam};
Instead - you are using an = instead of a : in your object literal. You assign properties of objects in literals using :.
Check out more info here.
Future reference
Try JsHint or JsLint to verify your code!
Also, if you have clean and organized code, it can make it easier to spot small errors like this, as well as improve the error messages you get (as your error will likely be on a shorter line). Using tools like JsBeautifier can get this done easily.
This would be your code after going through JS Beautifier:
var catParam = "(id=cat00000)";
var inputParams = {
serviceID: "getCategories",
apiKey: "asdfasfgx6",
catCriterior: catParam
};
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 thought that this would be easy but this does not work, though it doesn't give an error either.
I have some text on my web page with id's display1, display2, display3....
When I try to update them in a loop like this nothing happens.
for (i = 1; i < 4; i++){
('display' + i).innerHTML = "123";
}
What am I doing wrong? Is there a better way of doing this?
Try
for (i = 1; i < 4; i++){
document.getElementById('display' + i).innerHTML = "123";
}
Or (better practice), I recommend you give all your display elements one class name and
displays=document.getElementsByClassName('newClassNameOfDisplays');
for (i = 1; i < displays.length+1; i++){
displays[i].innerHTML = "123";
}
Your code didn't work because String().innerHTML doesn't exist, you had to get the element from the DOM rather than create a string. :)
The answers given in the comments are probably both correct (not sure why they weren't posted as answers). What is happening in your code is that you are creating a string (('display')+i), and then creating a new property called innerHTML on that string, and then setting the value of the new innerHTML property to "".
The reason this doesn't give an error is that (almost) everything in JavaScript is an object, and you can set properties on objects at will. For example ("Hello").description="a greeting" is a perfectly valid JavaScript statement.
In your code you're not properly selecting the elements, try this:
for (var i = 1; i < 4; i++) {
document.getElementById('display' + i).innerHTML = '123';
}
JSFiddle Example
http://jsfiddle.net/tLPkj/3/