0

I have 3 javascript functions:

validateClk(), validateAmPm() and getClks()

And on two occurring events, they get executed as follows:

OnChange - executes validateClk() and validateAmPm()

OnClick - executes getClks() (getClks() returns a boolean value)

All 3 functions run correctly, but the problem is, after getClks() has finished execution and returns a boolean, the next function postClocks() doesn't run. I'm very sure that the code for postClocks() is correct as well. If I don't use the return statement for getClks() then the getClks() function doesn't work as expected.

Please help :(

<script type='text/javascript'>
    function validateClk() {
        ....
        var clks = clocks.value;
        if (clks == "") {
            alert('Enter time');
        }
        else { ... }
    }
</script>

<script type='...'>
    function validateAMPM() {
        ...
        var ampm= ap.value;
        if  (ampm=="") {
            alert('Enter am or pm');
        }
    }
</script>

<script type='text/...'>
    function getClks() {
        var clks= clock.value;
        var ampm= ap.value;
        if (clks==" && ampm="") {
            alert('Enter time and am/pm');
            return false;
        }
        else { ... }
        return true;
    }
</script>

<... onChange="validateClk(); validateAmPm();" />

<... button label="Submit" onClick="return getClks(); postClocks(); return false;" />

3 Answers 3

4

It's because you explicitly coded a return in there.

return getClks();
postClocks();
return false;

That code will always just exit after that first return statement. I suggest removing it.

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

Comments

2

have all of your custom functions return boolean then change the onclick event to this:

onClick="if(!getClks() || !postClocks()) return false;"

assuming you don't want to continue if invalid

Comments

0

You wrote

onClick="return getClks(); postClocks(); return false;"

You have to remove the first "return".

3 Comments

Thanks guys:) But, like I said earlier, if I remove the 1st return statement and just write onClick='getClks(); postClocks();return false;' then, my getClks() function doesn't display the alerts as expected.
hmm, ok... what is the expected behaviour and what happens instead? I think the getClks should have the same behaviour, beacause the "return" has no effect on the code inside getClks.
Thankyou for helping, hacksteak25! :) I tried K Ivanov's suggestion and it works. My problem is solved :)

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.