uno
uno

Reputation: 99

Javascript functions

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;" />

Upvotes: 0

Views: 168

Answers (3)

Kris Ivanov
Kris Ivanov

Reputation: 10598

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

Upvotes: 2

hacksteak25
hacksteak25

Reputation: 2039

You wrote

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

You have to remove the first "return".

Upvotes: 0

Pointy
Pointy

Reputation: 413702

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.

Upvotes: 4

Related Questions