programmatically construct condition for use in if() statement











up vote
6
down vote

favorite












Let's assume for a moment that I have something like this:



if( document.getElementById('div1').innerHTML &&
document.getElementById('div2').innerHTML &&
document.getElementById('div3').innerHTML &&
document.getElementById('div4').innerHTML &&
document.getElementById('div5').innerHTML &&
...
document.getElementById('div100').innerHTML)


Obviously typing out and maintaining a big conditional statement like this is problematic.



What I would like is some solution like:



var conditional = "";
for(var i = 1; i <= 100; i++){
conditional += "document.getElementById('div" + i +"').innerHTML";
if(i < 100) {
conditional += " && ";
}
}
if(interpretStringAsJSExpression(conditional)){
console.log("all my divs have content");
}


Is something like this possible in Javascript?



Edit:



Great answers have been submitted, and I am sure that I and others will benefit from them. However, purely from a place of curiosity, is it possible to store and run Javascript expressions or commands in strings? Like I have proposed in my example: interpretStringAsJSExpression(conditional)










share|improve this question




















  • 1




    You can execute a string with eval(conditional). It's almost always the wrong solution, though.
    – Barmar
    3 hours ago

















up vote
6
down vote

favorite












Let's assume for a moment that I have something like this:



if( document.getElementById('div1').innerHTML &&
document.getElementById('div2').innerHTML &&
document.getElementById('div3').innerHTML &&
document.getElementById('div4').innerHTML &&
document.getElementById('div5').innerHTML &&
...
document.getElementById('div100').innerHTML)


Obviously typing out and maintaining a big conditional statement like this is problematic.



What I would like is some solution like:



var conditional = "";
for(var i = 1; i <= 100; i++){
conditional += "document.getElementById('div" + i +"').innerHTML";
if(i < 100) {
conditional += " && ";
}
}
if(interpretStringAsJSExpression(conditional)){
console.log("all my divs have content");
}


Is something like this possible in Javascript?



Edit:



Great answers have been submitted, and I am sure that I and others will benefit from them. However, purely from a place of curiosity, is it possible to store and run Javascript expressions or commands in strings? Like I have proposed in my example: interpretStringAsJSExpression(conditional)










share|improve this question




















  • 1




    You can execute a string with eval(conditional). It's almost always the wrong solution, though.
    – Barmar
    3 hours ago















up vote
6
down vote

favorite









up vote
6
down vote

favorite











Let's assume for a moment that I have something like this:



if( document.getElementById('div1').innerHTML &&
document.getElementById('div2').innerHTML &&
document.getElementById('div3').innerHTML &&
document.getElementById('div4').innerHTML &&
document.getElementById('div5').innerHTML &&
...
document.getElementById('div100').innerHTML)


Obviously typing out and maintaining a big conditional statement like this is problematic.



What I would like is some solution like:



var conditional = "";
for(var i = 1; i <= 100; i++){
conditional += "document.getElementById('div" + i +"').innerHTML";
if(i < 100) {
conditional += " && ";
}
}
if(interpretStringAsJSExpression(conditional)){
console.log("all my divs have content");
}


Is something like this possible in Javascript?



Edit:



Great answers have been submitted, and I am sure that I and others will benefit from them. However, purely from a place of curiosity, is it possible to store and run Javascript expressions or commands in strings? Like I have proposed in my example: interpretStringAsJSExpression(conditional)










share|improve this question















Let's assume for a moment that I have something like this:



if( document.getElementById('div1').innerHTML &&
document.getElementById('div2').innerHTML &&
document.getElementById('div3').innerHTML &&
document.getElementById('div4').innerHTML &&
document.getElementById('div5').innerHTML &&
...
document.getElementById('div100').innerHTML)


Obviously typing out and maintaining a big conditional statement like this is problematic.



What I would like is some solution like:



var conditional = "";
for(var i = 1; i <= 100; i++){
conditional += "document.getElementById('div" + i +"').innerHTML";
if(i < 100) {
conditional += " && ";
}
}
if(interpretStringAsJSExpression(conditional)){
console.log("all my divs have content");
}


Is something like this possible in Javascript?



Edit:



Great answers have been submitted, and I am sure that I and others will benefit from them. However, purely from a place of curiosity, is it possible to store and run Javascript expressions or commands in strings? Like I have proposed in my example: interpretStringAsJSExpression(conditional)







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago

























asked 4 hours ago









WillD

19211




19211








  • 1




    You can execute a string with eval(conditional). It's almost always the wrong solution, though.
    – Barmar
    3 hours ago
















  • 1




    You can execute a string with eval(conditional). It's almost always the wrong solution, though.
    – Barmar
    3 hours ago










1




1




You can execute a string with eval(conditional). It's almost always the wrong solution, though.
– Barmar
3 hours ago






You can execute a string with eval(conditional). It's almost always the wrong solution, though.
– Barmar
3 hours ago














5 Answers
5






active

oldest

votes

















up vote
1
down vote



accepted










As the other answers said, you can solve your conditions problem more easily.



But, to answer your new question




purely from a place of curiosity, is it possible to store and run
Javascript expressions or commands in strings?




Yes, you can write javascript to a string and execute it later with eval. Which you should not do if you are concerned with security or performance.






share|improve this answer





















  • Since this most closely answers my question I am going to accept it, though I recognize that for the example I gave there are more direct solutions, and furthermore that eval() has its own problems.
    – WillD
    2 hours ago


















up vote
5
down vote













You can do the tests in a loop.



var allOK = true;
for (var i = 1; i <= 100; i++) {
if (!document.getElementById("div"+i).innerHTML) {
allOK = false;
break;
}
}
if (allOK) {
console.log("all my divs have content");
}


You could also give all your DIVs a common class, then use a built-in iterator.



var allDivs = document.getElementsByClassName("divClass");
if (Array.from(allDivs).every(div => div.innerHTML)) {
console.log("all my divs have content");
}





share|improve this answer























  • I suppose document.getElementById("div"+i).innerHTML will return a string not a boolean. isn't it?
    – Gaurav
    4 hours ago






  • 1




    @Gaurav An empty string is falsey.
    – Barmar
    4 hours ago










  • +1, slow typing on my part and this is how I would do it. Didn't know about the built in iterator method. very cool!
    – billynoah
    4 hours ago


















up vote
4
down vote













Why interpret a string of code. There are other means like for loops:



var conditionResult = true;
for(var i = 1; i < 101; i++) {
conditionResult = conditionResult && document.getElementById('div' + i).innerHTML;
}

if(conditionResult) {
// do something here
}


You can also use array methods like some and every if you have the elements in an array:



var arr = [/* array of DOM elements */];

var conditionResult = arr.every(elem => elem.innerHTML); // this is equivalent to (innerHTML && innerHTML && ...)

var conditionResult = arr.some(elem => elem.innerHTML); // this is equivalent to (innerHTML || innerHTML || ...)





share|improve this answer




























    up vote
    2
    down vote













    Use document.querySelectorAll for this type of operation






    // Get all the divs that have ids which start with div
    var theDivs = document.querySelectorAll('[id^="div"]');
    var i,l,el,divsWithContent = ;

    // Loop through all theDivs
    l = theDivs.length;
    for(i = 0; i < l; i++) {
    // el is the div
    el = theDivs[i];

    // Test to make sure the id is div followed by one or more digits
    if (/^divd+$/.test(el.id)) {
    // If the div has something in it other than spaces, it's got content
    if (el.textContent.trim() !== "") {
    // Save the divs with content in the array
    divsWithContent.push(el.id);
    }
    }
    }

    // Show the results
    document.getElementById("result").textContent = divsWithContent.join("n");

    <h1>Div test</h1>
    <div id="div1">This</div>
    <div id="div2">that</div>
    <div id="div3"></div>
    <div id="div4">and</div>
    <div id="div5">the other thing</div>
    <h2>Divs with content</h2>
    <pre id="result"></pre>





    Ref: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll






    share|improve this answer




























      up vote
      1
      down vote













      You can set condition to true and check each one, setting condition to false and break out of the loop if any are false.



      var conditional = true;
      for(var i = 1; i <= 100; i++){
      if (!document.getElementById('div' + i).innerHTML) {
      condition = false;
      break;
      }
      }





      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%2f53689001%2fprogrammatically-construct-condition-for-use-in-if-statement%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        As the other answers said, you can solve your conditions problem more easily.



        But, to answer your new question




        purely from a place of curiosity, is it possible to store and run
        Javascript expressions or commands in strings?




        Yes, you can write javascript to a string and execute it later with eval. Which you should not do if you are concerned with security or performance.






        share|improve this answer





















        • Since this most closely answers my question I am going to accept it, though I recognize that for the example I gave there are more direct solutions, and furthermore that eval() has its own problems.
          – WillD
          2 hours ago















        up vote
        1
        down vote



        accepted










        As the other answers said, you can solve your conditions problem more easily.



        But, to answer your new question




        purely from a place of curiosity, is it possible to store and run
        Javascript expressions or commands in strings?




        Yes, you can write javascript to a string and execute it later with eval. Which you should not do if you are concerned with security or performance.






        share|improve this answer





















        • Since this most closely answers my question I am going to accept it, though I recognize that for the example I gave there are more direct solutions, and furthermore that eval() has its own problems.
          – WillD
          2 hours ago













        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        As the other answers said, you can solve your conditions problem more easily.



        But, to answer your new question




        purely from a place of curiosity, is it possible to store and run
        Javascript expressions or commands in strings?




        Yes, you can write javascript to a string and execute it later with eval. Which you should not do if you are concerned with security or performance.






        share|improve this answer












        As the other answers said, you can solve your conditions problem more easily.



        But, to answer your new question




        purely from a place of curiosity, is it possible to store and run
        Javascript expressions or commands in strings?




        Yes, you can write javascript to a string and execute it later with eval. Which you should not do if you are concerned with security or performance.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 3 hours ago









        jorbuedo

        1766




        1766












        • Since this most closely answers my question I am going to accept it, though I recognize that for the example I gave there are more direct solutions, and furthermore that eval() has its own problems.
          – WillD
          2 hours ago


















        • Since this most closely answers my question I am going to accept it, though I recognize that for the example I gave there are more direct solutions, and furthermore that eval() has its own problems.
          – WillD
          2 hours ago
















        Since this most closely answers my question I am going to accept it, though I recognize that for the example I gave there are more direct solutions, and furthermore that eval() has its own problems.
        – WillD
        2 hours ago




        Since this most closely answers my question I am going to accept it, though I recognize that for the example I gave there are more direct solutions, and furthermore that eval() has its own problems.
        – WillD
        2 hours ago












        up vote
        5
        down vote













        You can do the tests in a loop.



        var allOK = true;
        for (var i = 1; i <= 100; i++) {
        if (!document.getElementById("div"+i).innerHTML) {
        allOK = false;
        break;
        }
        }
        if (allOK) {
        console.log("all my divs have content");
        }


        You could also give all your DIVs a common class, then use a built-in iterator.



        var allDivs = document.getElementsByClassName("divClass");
        if (Array.from(allDivs).every(div => div.innerHTML)) {
        console.log("all my divs have content");
        }





        share|improve this answer























        • I suppose document.getElementById("div"+i).innerHTML will return a string not a boolean. isn't it?
          – Gaurav
          4 hours ago






        • 1




          @Gaurav An empty string is falsey.
          – Barmar
          4 hours ago










        • +1, slow typing on my part and this is how I would do it. Didn't know about the built in iterator method. very cool!
          – billynoah
          4 hours ago















        up vote
        5
        down vote













        You can do the tests in a loop.



        var allOK = true;
        for (var i = 1; i <= 100; i++) {
        if (!document.getElementById("div"+i).innerHTML) {
        allOK = false;
        break;
        }
        }
        if (allOK) {
        console.log("all my divs have content");
        }


        You could also give all your DIVs a common class, then use a built-in iterator.



        var allDivs = document.getElementsByClassName("divClass");
        if (Array.from(allDivs).every(div => div.innerHTML)) {
        console.log("all my divs have content");
        }





        share|improve this answer























        • I suppose document.getElementById("div"+i).innerHTML will return a string not a boolean. isn't it?
          – Gaurav
          4 hours ago






        • 1




          @Gaurav An empty string is falsey.
          – Barmar
          4 hours ago










        • +1, slow typing on my part and this is how I would do it. Didn't know about the built in iterator method. very cool!
          – billynoah
          4 hours ago













        up vote
        5
        down vote










        up vote
        5
        down vote









        You can do the tests in a loop.



        var allOK = true;
        for (var i = 1; i <= 100; i++) {
        if (!document.getElementById("div"+i).innerHTML) {
        allOK = false;
        break;
        }
        }
        if (allOK) {
        console.log("all my divs have content");
        }


        You could also give all your DIVs a common class, then use a built-in iterator.



        var allDivs = document.getElementsByClassName("divClass");
        if (Array.from(allDivs).every(div => div.innerHTML)) {
        console.log("all my divs have content");
        }





        share|improve this answer














        You can do the tests in a loop.



        var allOK = true;
        for (var i = 1; i <= 100; i++) {
        if (!document.getElementById("div"+i).innerHTML) {
        allOK = false;
        break;
        }
        }
        if (allOK) {
        console.log("all my divs have content");
        }


        You could also give all your DIVs a common class, then use a built-in iterator.



        var allDivs = document.getElementsByClassName("divClass");
        if (Array.from(allDivs).every(div => div.innerHTML)) {
        console.log("all my divs have content");
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 4 hours ago

























        answered 4 hours ago









        Barmar

        415k34239340




        415k34239340












        • I suppose document.getElementById("div"+i).innerHTML will return a string not a boolean. isn't it?
          – Gaurav
          4 hours ago






        • 1




          @Gaurav An empty string is falsey.
          – Barmar
          4 hours ago










        • +1, slow typing on my part and this is how I would do it. Didn't know about the built in iterator method. very cool!
          – billynoah
          4 hours ago


















        • I suppose document.getElementById("div"+i).innerHTML will return a string not a boolean. isn't it?
          – Gaurav
          4 hours ago






        • 1




          @Gaurav An empty string is falsey.
          – Barmar
          4 hours ago










        • +1, slow typing on my part and this is how I would do it. Didn't know about the built in iterator method. very cool!
          – billynoah
          4 hours ago
















        I suppose document.getElementById("div"+i).innerHTML will return a string not a boolean. isn't it?
        – Gaurav
        4 hours ago




        I suppose document.getElementById("div"+i).innerHTML will return a string not a boolean. isn't it?
        – Gaurav
        4 hours ago




        1




        1




        @Gaurav An empty string is falsey.
        – Barmar
        4 hours ago




        @Gaurav An empty string is falsey.
        – Barmar
        4 hours ago












        +1, slow typing on my part and this is how I would do it. Didn't know about the built in iterator method. very cool!
        – billynoah
        4 hours ago




        +1, slow typing on my part and this is how I would do it. Didn't know about the built in iterator method. very cool!
        – billynoah
        4 hours ago










        up vote
        4
        down vote













        Why interpret a string of code. There are other means like for loops:



        var conditionResult = true;
        for(var i = 1; i < 101; i++) {
        conditionResult = conditionResult && document.getElementById('div' + i).innerHTML;
        }

        if(conditionResult) {
        // do something here
        }


        You can also use array methods like some and every if you have the elements in an array:



        var arr = [/* array of DOM elements */];

        var conditionResult = arr.every(elem => elem.innerHTML); // this is equivalent to (innerHTML && innerHTML && ...)

        var conditionResult = arr.some(elem => elem.innerHTML); // this is equivalent to (innerHTML || innerHTML || ...)





        share|improve this answer

























          up vote
          4
          down vote













          Why interpret a string of code. There are other means like for loops:



          var conditionResult = true;
          for(var i = 1; i < 101; i++) {
          conditionResult = conditionResult && document.getElementById('div' + i).innerHTML;
          }

          if(conditionResult) {
          // do something here
          }


          You can also use array methods like some and every if you have the elements in an array:



          var arr = [/* array of DOM elements */];

          var conditionResult = arr.every(elem => elem.innerHTML); // this is equivalent to (innerHTML && innerHTML && ...)

          var conditionResult = arr.some(elem => elem.innerHTML); // this is equivalent to (innerHTML || innerHTML || ...)





          share|improve this answer























            up vote
            4
            down vote










            up vote
            4
            down vote









            Why interpret a string of code. There are other means like for loops:



            var conditionResult = true;
            for(var i = 1; i < 101; i++) {
            conditionResult = conditionResult && document.getElementById('div' + i).innerHTML;
            }

            if(conditionResult) {
            // do something here
            }


            You can also use array methods like some and every if you have the elements in an array:



            var arr = [/* array of DOM elements */];

            var conditionResult = arr.every(elem => elem.innerHTML); // this is equivalent to (innerHTML && innerHTML && ...)

            var conditionResult = arr.some(elem => elem.innerHTML); // this is equivalent to (innerHTML || innerHTML || ...)





            share|improve this answer












            Why interpret a string of code. There are other means like for loops:



            var conditionResult = true;
            for(var i = 1; i < 101; i++) {
            conditionResult = conditionResult && document.getElementById('div' + i).innerHTML;
            }

            if(conditionResult) {
            // do something here
            }


            You can also use array methods like some and every if you have the elements in an array:



            var arr = [/* array of DOM elements */];

            var conditionResult = arr.every(elem => elem.innerHTML); // this is equivalent to (innerHTML && innerHTML && ...)

            var conditionResult = arr.some(elem => elem.innerHTML); // this is equivalent to (innerHTML || innerHTML || ...)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 4 hours ago









            ibrahim mahrir

            21.3k41746




            21.3k41746






















                up vote
                2
                down vote













                Use document.querySelectorAll for this type of operation






                // Get all the divs that have ids which start with div
                var theDivs = document.querySelectorAll('[id^="div"]');
                var i,l,el,divsWithContent = ;

                // Loop through all theDivs
                l = theDivs.length;
                for(i = 0; i < l; i++) {
                // el is the div
                el = theDivs[i];

                // Test to make sure the id is div followed by one or more digits
                if (/^divd+$/.test(el.id)) {
                // If the div has something in it other than spaces, it's got content
                if (el.textContent.trim() !== "") {
                // Save the divs with content in the array
                divsWithContent.push(el.id);
                }
                }
                }

                // Show the results
                document.getElementById("result").textContent = divsWithContent.join("n");

                <h1>Div test</h1>
                <div id="div1">This</div>
                <div id="div2">that</div>
                <div id="div3"></div>
                <div id="div4">and</div>
                <div id="div5">the other thing</div>
                <h2>Divs with content</h2>
                <pre id="result"></pre>





                Ref: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll






                share|improve this answer

























                  up vote
                  2
                  down vote













                  Use document.querySelectorAll for this type of operation






                  // Get all the divs that have ids which start with div
                  var theDivs = document.querySelectorAll('[id^="div"]');
                  var i,l,el,divsWithContent = ;

                  // Loop through all theDivs
                  l = theDivs.length;
                  for(i = 0; i < l; i++) {
                  // el is the div
                  el = theDivs[i];

                  // Test to make sure the id is div followed by one or more digits
                  if (/^divd+$/.test(el.id)) {
                  // If the div has something in it other than spaces, it's got content
                  if (el.textContent.trim() !== "") {
                  // Save the divs with content in the array
                  divsWithContent.push(el.id);
                  }
                  }
                  }

                  // Show the results
                  document.getElementById("result").textContent = divsWithContent.join("n");

                  <h1>Div test</h1>
                  <div id="div1">This</div>
                  <div id="div2">that</div>
                  <div id="div3"></div>
                  <div id="div4">and</div>
                  <div id="div5">the other thing</div>
                  <h2>Divs with content</h2>
                  <pre id="result"></pre>





                  Ref: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll






                  share|improve this answer























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Use document.querySelectorAll for this type of operation






                    // Get all the divs that have ids which start with div
                    var theDivs = document.querySelectorAll('[id^="div"]');
                    var i,l,el,divsWithContent = ;

                    // Loop through all theDivs
                    l = theDivs.length;
                    for(i = 0; i < l; i++) {
                    // el is the div
                    el = theDivs[i];

                    // Test to make sure the id is div followed by one or more digits
                    if (/^divd+$/.test(el.id)) {
                    // If the div has something in it other than spaces, it's got content
                    if (el.textContent.trim() !== "") {
                    // Save the divs with content in the array
                    divsWithContent.push(el.id);
                    }
                    }
                    }

                    // Show the results
                    document.getElementById("result").textContent = divsWithContent.join("n");

                    <h1>Div test</h1>
                    <div id="div1">This</div>
                    <div id="div2">that</div>
                    <div id="div3"></div>
                    <div id="div4">and</div>
                    <div id="div5">the other thing</div>
                    <h2>Divs with content</h2>
                    <pre id="result"></pre>





                    Ref: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll






                    share|improve this answer












                    Use document.querySelectorAll for this type of operation






                    // Get all the divs that have ids which start with div
                    var theDivs = document.querySelectorAll('[id^="div"]');
                    var i,l,el,divsWithContent = ;

                    // Loop through all theDivs
                    l = theDivs.length;
                    for(i = 0; i < l; i++) {
                    // el is the div
                    el = theDivs[i];

                    // Test to make sure the id is div followed by one or more digits
                    if (/^divd+$/.test(el.id)) {
                    // If the div has something in it other than spaces, it's got content
                    if (el.textContent.trim() !== "") {
                    // Save the divs with content in the array
                    divsWithContent.push(el.id);
                    }
                    }
                    }

                    // Show the results
                    document.getElementById("result").textContent = divsWithContent.join("n");

                    <h1>Div test</h1>
                    <div id="div1">This</div>
                    <div id="div2">that</div>
                    <div id="div3"></div>
                    <div id="div4">and</div>
                    <div id="div5">the other thing</div>
                    <h2>Divs with content</h2>
                    <pre id="result"></pre>





                    Ref: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll






                    // Get all the divs that have ids which start with div
                    var theDivs = document.querySelectorAll('[id^="div"]');
                    var i,l,el,divsWithContent = ;

                    // Loop through all theDivs
                    l = theDivs.length;
                    for(i = 0; i < l; i++) {
                    // el is the div
                    el = theDivs[i];

                    // Test to make sure the id is div followed by one or more digits
                    if (/^divd+$/.test(el.id)) {
                    // If the div has something in it other than spaces, it's got content
                    if (el.textContent.trim() !== "") {
                    // Save the divs with content in the array
                    divsWithContent.push(el.id);
                    }
                    }
                    }

                    // Show the results
                    document.getElementById("result").textContent = divsWithContent.join("n");

                    <h1>Div test</h1>
                    <div id="div1">This</div>
                    <div id="div2">that</div>
                    <div id="div3"></div>
                    <div id="div4">and</div>
                    <div id="div5">the other thing</div>
                    <h2>Divs with content</h2>
                    <pre id="result"></pre>





                    // Get all the divs that have ids which start with div
                    var theDivs = document.querySelectorAll('[id^="div"]');
                    var i,l,el,divsWithContent = ;

                    // Loop through all theDivs
                    l = theDivs.length;
                    for(i = 0; i < l; i++) {
                    // el is the div
                    el = theDivs[i];

                    // Test to make sure the id is div followed by one or more digits
                    if (/^divd+$/.test(el.id)) {
                    // If the div has something in it other than spaces, it's got content
                    if (el.textContent.trim() !== "") {
                    // Save the divs with content in the array
                    divsWithContent.push(el.id);
                    }
                    }
                    }

                    // Show the results
                    document.getElementById("result").textContent = divsWithContent.join("n");

                    <h1>Div test</h1>
                    <div id="div1">This</div>
                    <div id="div2">that</div>
                    <div id="div3"></div>
                    <div id="div4">and</div>
                    <div id="div5">the other thing</div>
                    <h2>Divs with content</h2>
                    <pre id="result"></pre>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 3 hours ago









                    user2182349

                    7,01311632




                    7,01311632






















                        up vote
                        1
                        down vote













                        You can set condition to true and check each one, setting condition to false and break out of the loop if any are false.



                        var conditional = true;
                        for(var i = 1; i <= 100; i++){
                        if (!document.getElementById('div' + i).innerHTML) {
                        condition = false;
                        break;
                        }
                        }





                        share|improve this answer

























                          up vote
                          1
                          down vote













                          You can set condition to true and check each one, setting condition to false and break out of the loop if any are false.



                          var conditional = true;
                          for(var i = 1; i <= 100; i++){
                          if (!document.getElementById('div' + i).innerHTML) {
                          condition = false;
                          break;
                          }
                          }





                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            You can set condition to true and check each one, setting condition to false and break out of the loop if any are false.



                            var conditional = true;
                            for(var i = 1; i <= 100; i++){
                            if (!document.getElementById('div' + i).innerHTML) {
                            condition = false;
                            break;
                            }
                            }





                            share|improve this answer












                            You can set condition to true and check each one, setting condition to false and break out of the loop if any are false.



                            var conditional = true;
                            for(var i = 1; i <= 100; i++){
                            if (!document.getElementById('div' + i).innerHTML) {
                            condition = false;
                            break;
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 4 hours ago









                            billynoah

                            10.2k54261




                            10.2k54261






























                                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%2f53689001%2fprogrammatically-construct-condition-for-use-in-if-statement%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

                                What visual should I use to simply compare current year value vs last year in Power BI desktop

                                How to ignore python UserWarning in pytest?

                                Alexandru Averescu