views:

402

answers:

2

Hi,

I have such a code block and when code founds 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.

+1  A: 
$(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?

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

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");
    });
})
Jonathan Fingland
I did but it didn't work out, I was expecting tho.
Braveyard
the alert popped up, but then continued?
Jonathan Fingland
@Aaron: wtf, really?
Crescent Fresh
@Yeah, I didn't know why ? Maybe some comparability issue with Mozilla ?
Braveyard
@crescen.. if I knew wtf is happening, I wouldn't be here asking this question :|
Braveyard
Alright people it now works, because there was another file referred to that page that why it wasn't working.
Braveyard
thanks and glad it was resolved
Jonathan Fingland
@Jonathan, Thanks to you :)
Braveyard