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.

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

0
0

You wrote

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

You have to remove the first "return".

3
  • 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.
    – uno
    Feb 14, 2011 at 16:27
  • 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. Feb 14, 2011 at 16:33
  • Thankyou for helping, hacksteak25! :) I tried K Ivanov's suggestion and it works. My problem is solved :)
    – uno
    Feb 14, 2011 at 16:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.