Can't use .insertAdjacentHTML within a nested for loop
up vote
0
down vote
favorite
As the title says, I can't use .insertAdjacentHTML because I just don't get how.
for(let i=1;i<=m;i++)
{
for(let j=1;j<=n;j++)
{
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + 'j" size="3" maxlength="4" inputmode="numeric"></input>')
}
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<br/>')
}
}
I want the input classes to be like l1c1 l1c2 l1c3 and so on but I can't seem to get that j to work as the variable. It is being used as a string so all my classes are l1cj l2cj and so on.
The fact that I have to use so many quotation marks really confuses me.
How should I use it to get the desired effect?
javascript
add a comment |
up vote
0
down vote
favorite
As the title says, I can't use .insertAdjacentHTML because I just don't get how.
for(let i=1;i<=m;i++)
{
for(let j=1;j<=n;j++)
{
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + 'j" size="3" maxlength="4" inputmode="numeric"></input>')
}
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<br/>')
}
}
I want the input classes to be like l1c1 l1c2 l1c3 and so on but I can't seem to get that j to work as the variable. It is being used as a string so all my classes are l1cj l2cj and so on.
The fact that I have to use so many quotation marks really confuses me.
How should I use it to get the desired effect?
javascript
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
As the title says, I can't use .insertAdjacentHTML because I just don't get how.
for(let i=1;i<=m;i++)
{
for(let j=1;j<=n;j++)
{
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + 'j" size="3" maxlength="4" inputmode="numeric"></input>')
}
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<br/>')
}
}
I want the input classes to be like l1c1 l1c2 l1c3 and so on but I can't seem to get that j to work as the variable. It is being used as a string so all my classes are l1cj l2cj and so on.
The fact that I have to use so many quotation marks really confuses me.
How should I use it to get the desired effect?
javascript
As the title says, I can't use .insertAdjacentHTML because I just don't get how.
for(let i=1;i<=m;i++)
{
for(let j=1;j<=n;j++)
{
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + 'j" size="3" maxlength="4" inputmode="numeric"></input>')
}
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<br/>')
}
}
I want the input classes to be like l1c1 l1c2 l1c3 and so on but I can't seem to get that j to work as the variable. It is being used as a string so all my classes are l1cj l2cj and so on.
The fact that I have to use so many quotation marks really confuses me.
How should I use it to get the desired effect?
for(let i=1;i<=m;i++)
{
for(let j=1;j<=n;j++)
{
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + 'j" size="3" maxlength="4" inputmode="numeric"></input>')
}
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<br/>')
}
}
for(let i=1;i<=m;i++)
{
for(let j=1;j<=n;j++)
{
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + 'j" size="3" maxlength="4" inputmode="numeric"></input>')
}
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<br/>')
}
}
javascript
javascript
asked Nov 11 at 19:30
Octavian Niculescu
266
266
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
If I've got this right you're trying to create a maxtrix of l rows and c columns. Instead of attaching elements (as HTML) to the DOM on each loop iteration (which is costly performance-wise), just push each input into an array. Then you can join that array and use insertAdjacentHTML
to add the HTML to the page at the end.
Here I've used a template literal too so there is less confusion about where the quotes should go. I added a placeholder to the input to show you how the classes look so you don't have to inspect the page.
Hope it helps.
const arr = ;
const m = n = 3;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
const input = `
<input
class="l${i}c${j}
size="3"
maxlength="4"
inputmode="numeric"
placeholder="l${i}c${j}"
/>`;
arr.push(input);
}
}
const root = document.querySelector('#root')
root.insertAdjacentHTML('beforeend', arr.join(''));
* {
box-sizing: border-box;
}
#root {
max-width: 100px;
padding: 10px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
}
input {
-moz-appearance:textfield;
}
<div id="root"></div>
Can you please explain this? "l${i}c${j}"
– Octavian Niculescu
Nov 11 at 20:10
In a template literal variables can be inserted like${var}
. It means that you don't have to keep quote/unquoting the string to insert them.
– Andy
Nov 11 at 20:52
Sorry for the late response. Thanks for your time. I will try to use it now.
– Octavian Niculescu
Nov 18 at 17:23
add a comment |
up vote
0
down vote
Your quoting is wrong. j
should not be inside the quotes.
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + j + '" size="3" maxlength="4" inputmode="numeric"></input>')
The method in the other answer will be more efficient -- every time you call insertAdjacentHTML()
the browser has to parse the new HTML. Doing it all at once is generally better.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If I've got this right you're trying to create a maxtrix of l rows and c columns. Instead of attaching elements (as HTML) to the DOM on each loop iteration (which is costly performance-wise), just push each input into an array. Then you can join that array and use insertAdjacentHTML
to add the HTML to the page at the end.
Here I've used a template literal too so there is less confusion about where the quotes should go. I added a placeholder to the input to show you how the classes look so you don't have to inspect the page.
Hope it helps.
const arr = ;
const m = n = 3;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
const input = `
<input
class="l${i}c${j}
size="3"
maxlength="4"
inputmode="numeric"
placeholder="l${i}c${j}"
/>`;
arr.push(input);
}
}
const root = document.querySelector('#root')
root.insertAdjacentHTML('beforeend', arr.join(''));
* {
box-sizing: border-box;
}
#root {
max-width: 100px;
padding: 10px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
}
input {
-moz-appearance:textfield;
}
<div id="root"></div>
Can you please explain this? "l${i}c${j}"
– Octavian Niculescu
Nov 11 at 20:10
In a template literal variables can be inserted like${var}
. It means that you don't have to keep quote/unquoting the string to insert them.
– Andy
Nov 11 at 20:52
Sorry for the late response. Thanks for your time. I will try to use it now.
– Octavian Niculescu
Nov 18 at 17:23
add a comment |
up vote
0
down vote
accepted
If I've got this right you're trying to create a maxtrix of l rows and c columns. Instead of attaching elements (as HTML) to the DOM on each loop iteration (which is costly performance-wise), just push each input into an array. Then you can join that array and use insertAdjacentHTML
to add the HTML to the page at the end.
Here I've used a template literal too so there is less confusion about where the quotes should go. I added a placeholder to the input to show you how the classes look so you don't have to inspect the page.
Hope it helps.
const arr = ;
const m = n = 3;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
const input = `
<input
class="l${i}c${j}
size="3"
maxlength="4"
inputmode="numeric"
placeholder="l${i}c${j}"
/>`;
arr.push(input);
}
}
const root = document.querySelector('#root')
root.insertAdjacentHTML('beforeend', arr.join(''));
* {
box-sizing: border-box;
}
#root {
max-width: 100px;
padding: 10px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
}
input {
-moz-appearance:textfield;
}
<div id="root"></div>
Can you please explain this? "l${i}c${j}"
– Octavian Niculescu
Nov 11 at 20:10
In a template literal variables can be inserted like${var}
. It means that you don't have to keep quote/unquoting the string to insert them.
– Andy
Nov 11 at 20:52
Sorry for the late response. Thanks for your time. I will try to use it now.
– Octavian Niculescu
Nov 18 at 17:23
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If I've got this right you're trying to create a maxtrix of l rows and c columns. Instead of attaching elements (as HTML) to the DOM on each loop iteration (which is costly performance-wise), just push each input into an array. Then you can join that array and use insertAdjacentHTML
to add the HTML to the page at the end.
Here I've used a template literal too so there is less confusion about where the quotes should go. I added a placeholder to the input to show you how the classes look so you don't have to inspect the page.
Hope it helps.
const arr = ;
const m = n = 3;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
const input = `
<input
class="l${i}c${j}
size="3"
maxlength="4"
inputmode="numeric"
placeholder="l${i}c${j}"
/>`;
arr.push(input);
}
}
const root = document.querySelector('#root')
root.insertAdjacentHTML('beforeend', arr.join(''));
* {
box-sizing: border-box;
}
#root {
max-width: 100px;
padding: 10px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
}
input {
-moz-appearance:textfield;
}
<div id="root"></div>
If I've got this right you're trying to create a maxtrix of l rows and c columns. Instead of attaching elements (as HTML) to the DOM on each loop iteration (which is costly performance-wise), just push each input into an array. Then you can join that array and use insertAdjacentHTML
to add the HTML to the page at the end.
Here I've used a template literal too so there is less confusion about where the quotes should go. I added a placeholder to the input to show you how the classes look so you don't have to inspect the page.
Hope it helps.
const arr = ;
const m = n = 3;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
const input = `
<input
class="l${i}c${j}
size="3"
maxlength="4"
inputmode="numeric"
placeholder="l${i}c${j}"
/>`;
arr.push(input);
}
}
const root = document.querySelector('#root')
root.insertAdjacentHTML('beforeend', arr.join(''));
* {
box-sizing: border-box;
}
#root {
max-width: 100px;
padding: 10px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
}
input {
-moz-appearance:textfield;
}
<div id="root"></div>
const arr = ;
const m = n = 3;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
const input = `
<input
class="l${i}c${j}
size="3"
maxlength="4"
inputmode="numeric"
placeholder="l${i}c${j}"
/>`;
arr.push(input);
}
}
const root = document.querySelector('#root')
root.insertAdjacentHTML('beforeend', arr.join(''));
* {
box-sizing: border-box;
}
#root {
max-width: 100px;
padding: 10px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
}
input {
-moz-appearance:textfield;
}
<div id="root"></div>
const arr = ;
const m = n = 3;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
const input = `
<input
class="l${i}c${j}
size="3"
maxlength="4"
inputmode="numeric"
placeholder="l${i}c${j}"
/>`;
arr.push(input);
}
}
const root = document.querySelector('#root')
root.insertAdjacentHTML('beforeend', arr.join(''));
* {
box-sizing: border-box;
}
#root {
max-width: 100px;
padding: 10px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
}
input {
-moz-appearance:textfield;
}
<div id="root"></div>
answered Nov 11 at 19:50
Andy
28k63160
28k63160
Can you please explain this? "l${i}c${j}"
– Octavian Niculescu
Nov 11 at 20:10
In a template literal variables can be inserted like${var}
. It means that you don't have to keep quote/unquoting the string to insert them.
– Andy
Nov 11 at 20:52
Sorry for the late response. Thanks for your time. I will try to use it now.
– Octavian Niculescu
Nov 18 at 17:23
add a comment |
Can you please explain this? "l${i}c${j}"
– Octavian Niculescu
Nov 11 at 20:10
In a template literal variables can be inserted like${var}
. It means that you don't have to keep quote/unquoting the string to insert them.
– Andy
Nov 11 at 20:52
Sorry for the late response. Thanks for your time. I will try to use it now.
– Octavian Niculescu
Nov 18 at 17:23
Can you please explain this? "l${i}c${j}"
– Octavian Niculescu
Nov 11 at 20:10
Can you please explain this? "l${i}c${j}"
– Octavian Niculescu
Nov 11 at 20:10
In a template literal variables can be inserted like
${var}
. It means that you don't have to keep quote/unquoting the string to insert them.– Andy
Nov 11 at 20:52
In a template literal variables can be inserted like
${var}
. It means that you don't have to keep quote/unquoting the string to insert them.– Andy
Nov 11 at 20:52
Sorry for the late response. Thanks for your time. I will try to use it now.
– Octavian Niculescu
Nov 18 at 17:23
Sorry for the late response. Thanks for your time. I will try to use it now.
– Octavian Niculescu
Nov 18 at 17:23
add a comment |
up vote
0
down vote
Your quoting is wrong. j
should not be inside the quotes.
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + j + '" size="3" maxlength="4" inputmode="numeric"></input>')
The method in the other answer will be more efficient -- every time you call insertAdjacentHTML()
the browser has to parse the new HTML. Doing it all at once is generally better.
add a comment |
up vote
0
down vote
Your quoting is wrong. j
should not be inside the quotes.
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + j + '" size="3" maxlength="4" inputmode="numeric"></input>')
The method in the other answer will be more efficient -- every time you call insertAdjacentHTML()
the browser has to parse the new HTML. Doing it all at once is generally better.
add a comment |
up vote
0
down vote
up vote
0
down vote
Your quoting is wrong. j
should not be inside the quotes.
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + j + '" size="3" maxlength="4" inputmode="numeric"></input>')
The method in the other answer will be more efficient -- every time you call insertAdjacentHTML()
the browser has to parse the new HTML. Doing it all at once is generally better.
Your quoting is wrong. j
should not be inside the quotes.
document.querySelector(".matrice-" + p).insertAdjacentHTML('beforeend', '<input class="l' + i + 'c' + j + '" size="3" maxlength="4" inputmode="numeric"></input>')
The method in the other answer will be more efficient -- every time you call insertAdjacentHTML()
the browser has to parse the new HTML. Doing it all at once is generally better.
answered Nov 11 at 19:53
Barmar
415k34239340
415k34239340
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252403%2fcant-use-insertadjacenthtml-within-a-nested-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown