I have such a code block and when code finds an error attribute which is set to error, then it should prevent further execution, how can I prevent it from execution :
$(document).ready(function() {
$("#SendMessage").click(function(e) {
var elements = $("#contact-area input:text[error=error]").get();
for (var i = 0; i < elements.length; i++) {
if ($(elements[0]).attr("error") == "error") {
alert("error found");
// Stop execution of whole function
}
}
$("#loaderImage").show();
$("#statusMessage").text("Sending, Please wait...");
window.setTimeout(function() { PageMethods.SendMessage(new Message().fullContent(), onSuccess(), onFailed()) }, "4000");
});
})
And How can I do that with both JQuery and Javascript, maybe there is a pre-defined function to do that easily in JQuery.
asked Oct 9, 2009 at 0:26
Tarik
82.2k86 gold badges244 silver badges352 bronze badges
2 Answers 2
$(document).ready(function() {
var flag = false;
$("#SendMessage").click(function(e) {
var elements = $("#contact-area input:text[error=error]").get();
for (var i = 0; i < elements.length; i++) {
if ($(elements[0]).attr("error") == "error") {
alert("error found");
// Stop execution of whole function
flag = true;
break;
}
}
if (!flag){
$("#loaderImage").show();
$("#statusMessage").text("Sending, Please wait...");
window.setTimeout(function() { PageMethods.SendMessage(new Message().fullContent(), onSuccess(), onFailed()) }, "4000");
}
});
})
Would that accomplish what you need?
answered Oct 9, 2009 at 0:30
Chris Thompson
35.6k12 gold badges86 silver badges110 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Tarik
Well, actually I was expecting something like "what return does in C#", isn't there any function or keyword to prevent execution ? maybe stopExecution(); ?
just return; when the error is found
$(document).ready(function() {
$("#SendMessage").click(function(e) {
var elements = $("#contact-area input:text[error=error]").get();
for (var i = 0; i < elements.length; i++) {
if ($(elements[0]).attr("error") == "error") {
alert("error found");
return;
}
}
$("#loaderImage").show();
$("#statusMessage").text("Sending, Please wait...");
window.setTimeout(function() { PageMethods.SendMessage(new Message().fullContent(), onSuccess(), onFailed()) }, "4000");
});
})
answered Oct 9, 2009 at 0:31
Jonathan Fingland
57.3k11 gold badges89 silver badges79 bronze badges
4 Comments
Jonathan Fingland
the alert popped up, but then continued?
Tarik
@Yeah, I didn't know why ? Maybe some comparability issue with Mozilla ?
Tarik
@crescen.. if I knew wtf is happening, I wouldn't be here asking this question :|
Tarik
Alright people it now works, because there was another file referred to that page that why it wasn't working.
lang-js