+ 2
How to execute if statemen for many elements
For example i have 100 paragraf <p>0</p> <p>0</p> <p>0</p> <p>0</p> ..... <p>0</p> And i want to execute all those paragraphs using if statement like this : "" if all the paragraphs.innerHTML = 0 then...."" Can someone help me?? #javascript
2 Réponses
+ 3
//paragraphs should be an array of all paragraphs
var result = true;
for(var i=0;i<paragraphs.length;i++){
if(parseInt(paragraphs[i].innerHTML) != 0){
result = false;
break;
}
}
if(result){
//all are 0
}else{
//not all are 0
}
+ 4
you coud do it this way:
var allZeroes = true;
parag = document. getElementsByTagName("p");
for ( i = 0 ; i < parag. length ; i++) {
if (parag. [i].innerHTML ! == 0){
allZeroes = false;
break;
}
}
if(allZeroes) {
}