JS: Using changing String in command after "." [duplicate] - javascript

This question already has answers here:
Reference a javascript property with a dynamic name
(3 answers)
Closed 3 years ago.
Currently I'm trying out some html and js stuff.
I have trouble using a changing string inside a command with some "." in it.
for (let i = 1; i <= 2; i++) {
let count = String("q" + i);
console.log(document.calculate.$(this.count).value);
}
So the String "count" is changing and I want the different values of my document with the names "q1", "q2"...
Would be awesome if someone could help me.
Thank You!

put dinamic value in a [] quotes instead .
console.log(document.calculate[$(this.count)].value);

for (let i = 1; i<=2; i++) {
let count = String("q" + i);
console.log(document.calculate.$(this[count]).value);
}

Related

RegExp exec not working in for loop snippet - Javascript [duplicate]

This question already has answers here:
Why does a RegExp with global flag give wrong results?
(7 answers)
Closed 3 months ago.
Can someone help check this snippet? I'm just looping on array but the last item return false or null even its matches the pattern.
let texts = ["Invoice_001_1", "Invoice_001_2"];
var regTest = /(([a-zA-Z 0-9]+)_([0-9]+)_([0-9]+)$|[a-zA-Z ]+_([0-9]+)$)/gi;
for(var x=0; x<texts.length; x++) {
console.log(texts[x]);
var matchReg = regTest.exec(texts[x]);
console.log(matchReg);
}
JSfiddle Demo
The issue is that when using /g, js has a little gotcha, in that it tells the regexp to keep the index of the last match.The fix is to manually reset the index:
let texts = ["Invoice_001_1", "Invoice_001_2"];
var regTest = /(([a-zA-Z 0-9]+)_([0-9]+)_([0-9]+)$|[a-zA-Z ]+_([0-9]+)$)/gi;
for(var x=0; x<texts.length; x++) {
console.log(texts[x]);
var matchReg = regTest.exec(texts[x]);
console.log(matchReg);
regTest.lastIndex = 0;
}

Javascript to change color of all spans with certain ID [duplicate]

This question already has answers here:
Mulitple Elements with the same ID
(5 answers)
Closed 2 years ago.
I have the following code to change the color of text with a certain spanID. Currently, it only changes the first instance of the span and on subsequent instances. Any suggestions?
<script>
function spanColor() {
var x = document.getElementById('someId');
x.style.color = '#'+Math.random().toString(16).substr(-6);
}
</script>
If you want to return all the needed elements you should use querySelectorAll(#id)
But as you saw in the comments - ID is not the best way to get more than one item.
You should change the ID for a ClassName and use var x = document.getElementsByClassName("example");
So your code should look like this:
<script>
function spanColor() {
var x = document.getElementsByClassName('someClassname');
var i;
for (i = 0; i < x.length; i++) {
x[i].style.color = '#'+Math.random().toString(16).substr(-6);
}
}
</script>

javascript - extracting cell value [duplicate]

This question already has answers here:
JavaScript, getting value of a td with id name
(8 answers)
Closed 2 years ago.
I wrote some code that is scanning my HTML table, and I want to use it for formatting that table.
var count_rows = document.getElementById("currency_tab").rows.length;
for (i = 0; i <= count_rows; i++ ) {
var count_cells = document.getElementById("currency_tab").rows[i].cells.length;
for (j = 0; j <= count_cells; j++) {
var check_str = document.getElementById("currency_tab").rows[i].cells[j];
/*
console.log(check_str);
console.log(typeof(check_str));
*/
var check = check_str.includes("-")
if(check) {
check_str.style.color = "red";
} else {
check_str.style.color = "green";
}
}
}
js console.log(check_str); is returning not a value of cell but an object e.g. <th>CURRENCY</th>.
I have tried to parse it with check_str.slice but that is forcing me to count a length of chars in object. I hope there is easier method to resolve that.
You can get the text with check_str.textContent
Please refer to the following documentation: Node.textContent
Also, if you are unsure about the properties of an object you can log them with console.dir(check_str).

How to put a text-to-speech in a loop [duplicate]

This question already has answers here:
JavaScript variable binding and loop
(4 answers)
Closed 4 years ago.
I'm using the js library p5.speech. I'm trying to get each strings throught the speech function. However, each time it repeats the same strings.
So is the issue coming from my code or the js library (and in this case, what library should i use?)
const btReport1 = document.getElementById('report1');
btReport1.addEventListener('click', function(e) {
myVoice.setVoice('Google UK English Female');
for(var i=0; i<allData.length;i++){
console.log("allData"+i+" = " + allData[i].profile.length);
// myVoice.speak(allData[i].profile.length+" patients have "+ allData[i].food);;
setTimeout(function afterTwoSeconds() { myVoice.speak(allData[i].profile.length+" dogs have "+ allData[i].food);}, 1000);
}
});
In your for loop, try replacing var with let so that
for(var i=0; i<allData.length;i++){
becomes
for(let i=0; i<allData.length;i++){
For more info, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Javascript JSLint create n elements [duplicate]

This question already has answers here:
JSlint: unexpected 'for' [duplicate]
(2 answers)
Closed 7 years ago.
I'm new to JSLint and I'm trying to create function that outputs amount of elements specified in first argument. Normally I would use the for loop but JSLint doesn't like loops and complains about it.
I've searched the web looking for satisfying answer, but the only ones that I've found are with use of new Array or other way of outsmarting JSLint.
So, how to change this code to JSLint-friendly?
function createElements(amount) {
var i;
var elements = [];
for (i = 0; i < amount; i += 1) {
elements.push(document.createElement('div'));
}
return elements;
}
Try this code,
function createElements(amount, document) {
'use strict';
var i = 0;
var elements = [];
while (i < amount) {
i = i + 1;
elements.push(document.createElement('div'));
}
return elements;
}

Categories