Well, I'm trying to get him to make some fields, if any of them is equal, it's the message and, though my message is giving non-stop, direct, how can I make it stop giving a lot of alert?
function sendAll(){
for (i = 1;i <=10;i ++) {
for (o = 1;o <=10;o ++) {
if(document.getElementById("table" + i).value==document.getElementById("table" + o).value){
alert("Some field is equal, check again.");
break;
return false;
}
}
}
}
asked Feb 6, 2014 at 19:51
Leonardo Joksan
1152 silver badges6 bronze badges
4 Answers 4
You don't need break statement, just remove it. return statement will do the rest
use
function sendAll() {
for (i = 1; i <= 10; i++) {
for (o = 1; o <= 10; o++) {
if (document.getElementById("table" + i).value == document.getElementById("table" + o).value) {
alert("Some field is equal, check again.");
//break;
return false;
}
}
}
//If you are using for validation purpose return true
// as default
return true;
}
answered Feb 6, 2014 at 19:53
Satpal
134k13 gold badges168 silver badges171 bronze badges
Your break simply stops the inner-most loop, but allows the outer loop to continue. To break out of the function entirely, just use a return:
function sendAll(){
for (i = 1;i <=10;i ++) {
for (o = 1;o <=10;o ++) {
if(document.getElementById("table" + i).value==document.getElementById("table" + o).value){
alert("Some field is equal, check again.");
return false;
}
}
}
return true; // suggested by Mike W
}
answered Feb 6, 2014 at 19:53
p.s.w.g
150k31 gold badges307 silver badges339 bronze badges
2 Comments
p.s.w.g
@MikeW Good point. It's also possible OP is not actually be using the return value at all, in case
return false; should be return;.Try something like this
function sendAll(){
var bEqual = false;
for (i = 1;i <=10;i ++) {
for (o = 1;o <=10;o ++) {
if(document.getElementById("table" + i).value==document.getElementById("table" + o).value){
bEqual = true;
}
}
}
if (bEqual) {
alert("Some field is equal, check again.");
}
return bEqual;
}
answered Feb 6, 2014 at 19:53
BrettFromLA
9261 gold badge7 silver badges17 bronze badges
Comments
Perhaps you could do:
function sendAll(){
if (!fieldsOk()) {
alert('some message');
}
}
function fieldsOk() {
for (i = 1;i <=10;i ++) {
for (o = 1;o <=10;o ++) {
if(document.getElementById("table" + i).value==document.getElementById("table" + o).value){
return false;
}
}
}
return true;
}
Comments
lang-js
break: that makes it break from the inner loop (the outer loop will keep looping) before thereturnis executed.