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?










share|improve this question


























    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?










    share|improve this question
























      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 19:30









      Octavian Niculescu

      266




      266
























          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>








          share|improve this answer





















          • 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


















          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.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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>








            share|improve this answer





















            • 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















            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>








            share|improve this answer





















            • 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













            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>








            share|improve this answer












            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>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • 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












            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 19:53









                Barmar

                415k34239340




                415k34239340






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine