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.
I would like 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?
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)
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)
When we use in with if statements, our code can tell if a string contains a substring, whether a list has a specific value, or if a dictionary has a certain key. Our if statements can also make decisions based on the opposite. We do that with not in .
In JavaScript we have the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.
Conditional Code FormatThe statement begins with if followed by a condition in parentheses. After the condition, the code block is run. Code blocks are contained within curly braces { } . The else means "if the previous condition isn't met, do this instead," or more simply, "Otherwise."
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"); }
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 code to a string and execute it later with eval. Which you should not do if you are concerned with security or performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With