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

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]);
}

Related

Why some parts of this code are red in VS 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 3 months ago.
Improve this question
I just started to learn JavaScript. I have this script which should generate composite numbers in my separate HTML file, but I don't really understand why some parts are highlighted red when I moved script to .js file.
So my question is why there are some parts that are highlighted and how I'm suppose to write it correctly? I need to use strict in this script. I know that when using strict mode I need to declare variables, objects etc.
since you are working in a javascript file.
remove the script tags
you only need script tags in other file types like html etc
function checkComposite(num) {
var arr = [];
if( var num == 1 ) {
return false;
}
else if ( var num == 2) {
return false;
}
for (var x = 2; x < num; x++) {
if(num % x == 0) {
return true;
}
}
return false;
}
function compositeNumbers() {
num = Number(document.getElementsById('number').value);
for(var j = 1; j < num; j++) {
if(checkComposite(j)) {
arr.push(j);
}
}
document.getElementsById('result').innerHTML = arr;
}

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' ]

Iterate two arrays: Cannot read property 'length' of undefined [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
I want to iterate over two arrays, states and districts, but I get an error-message saying "Cannot read property 'length' of undefined". I don't know what's causing it.
This is my code:
var estados = [ "aguascalientes", "baja-california", "baja-california-sur",
"yucatan", "zacatecas", ];
var municipios = [ [ "aguascalientes", "asientos", "calvillo", "cosio", "tepezala" ],
[ "mexicali", "playas-de-rosarito", "ensenada" ] ];
$("#property_location").change(function () {
$( "#property_location option:selected" ).each(function() {
var i;
var j;
for (i = estados.length - 1; i >= 0; i--) {
if ($( this ).val() == estados[i]) {
console.log(estados[i]);
for (j = municipios[i].length - 1; i >= 0; i--) {
$("#property_municipio option[value='"+municipios[i][j]+"']").show();
}
} else {
for (j = municipios[i].length - 1; i >= 0; i--) {
$("#property_municipio option[value='"+municipios[i][j]+"']").hide();
}
}
}
});
}).change();
Your problem is that estados has more member than municipios you loop trough every member of estados and check how many member does the municipios sub array have.
for (j = municipios[i].length - 1; i >= 0; i--)
if i is greater than 1 municipios[i] is undefined and you can't calculate the length of undefined. this generate your error.
By the way, you overwrite your loop index i if you use it in both loops.
Please use it like that:
for(var i = 0; i < array.length; i++){
...
for(var j = 0; j < array.length; j++){
...
}
}
The first/top array is having 5 elements as I see, while the second array municipios is having 2 elements( ofcourse the nested lists are having data, but the actual length will be 2 as is it containing only 2 lists).
While the outer loop is working fine, the issue is with the inner loop. When i reaches greater than 2, suppose 3, the code is trying to fetch the 3rd element from municipios array, which does not even exist. Moreover, its trying to access that particular elements' length.

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>

Categories

Resources