1

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.

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Well, actually I was expecting something like "what return does in C#", isn't there any function or keyword to prevent execution ? maybe stopExecution(); ?
1

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");
    });
})

4 Comments

the alert popped up, but then continued?
@Yeah, I didn't know why ? Maybe some comparability issue with Mozilla ?
@crescen.. if I knew wtf is happening, I wouldn't be here asking this question :|
Alright people it now works, because there was another file referred to that page that why it wasn't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.