codeomnitrix
codeomnitrix

Reputation: 4249

problem with javascript: return false is not working

Hey there is a link in my program as shown and onclick it calls the function clearform as shown:

Html Code:

<a class="button" href="Cancel" style="left: 55%;" onclick="clearForm()">Cancel</a>

JavaScript Code:

function clearForm(){
        document.getElementById("subjectName").value = "";
        return false;
    }

return false is not working in this code. actually the first line of the function executed successfully but the return false was failed. I mean page is redirected to url "Cancel".

Upvotes: 6

Views: 27002

Answers (7)

Malhaar Punjabi
Malhaar Punjabi

Reputation: 783

Referring to this question:

JavaScript confirm cancel button still posting

I had the same issue, and also need to disable submit button when user click submit the form to prevent double submission. This is my code, and tested successfully.

<script type="text/javascript">
        function confirmSubmit(event) {

            if (!confirm('Are you certain you want to lodge this form ?')) {
                event.disabled = false;

                event.preventDefault();
                stopPropagation();

                return false;
            }
            return true;
        }
    </script>

 <span class="btn">
        <asp:Button ID="btnSubmit" runat="server" Text="Lodge this form now" UseSubmitBehavior="false" OnClientClick="this.disabled='true';confirmSubmit(this)" OnClick="Submit"  />
    </span>

Upvotes: 0

LuckyLuke Skywalker
LuckyLuke Skywalker

Reputation: 781

The return false; somehow needs to be right at the front.

(In ALL situations I've dealt with over the past months - may or may not be a bug).

Like this: onclick="return false; clearForm();"


Besides that, as mentioned by others as well, you need to return it from the onclick, not just from the function. In your case: onclick="return clearForm()".

Upvotes: 1

userup
userup

Reputation: 11

Keep in mind that some browser extensions may interfere with proper operation of links. For example, I ran into a situation where someone had both AdBlock Plus and Ghostery enabled. Clicking a simple 'a' tag with an 'onclick="return SomeFunction();"' attribute (the function returned false) caused the browser to treat the click as a page transition and went to a blank page and the loading indicator just kept spinning. Disabling those browser extensions cleared up the problem.

Including this as an answer because this was the first page I landed on from Google.

Upvotes: -1

mrbinky3000
mrbinky3000

Reputation: 4291

Please note that if there is a bug or error in clearForm() then "return false" will NOT stop the anchor action and your browser will try to link to the href "Cancel". Here is the logic:

  1. User clicks on anchor
  2. onClick fires clearForm()
  3. There is an error in clearForm() so Javascript crashes and stops all code execution.
  4. return false is never fired because Javascript has already stopped.

If you are relying on a third party JavaScript API (I was using code supplied by Recyclebank to spawn a popup), and the third party API makes an update that breaks the JavaScript, then you'll have problems.

The following will stop the link under normal conditions and error conditions.

<a class="button" href="javascript:;" style="left: 55%;" onclick="clearForm();return false;">Cancel</a>

Upvotes: 1

alex
alex

Reputation: 490143

Your problem is you need to return the Boolean.

But, drop all that...

  • Attach your event unobtrusively...

    element.onclick = clearForm;

  • Use preventDefault(). It is the modern way of acheiving that.

    function clearForm(event) { event.preventDefault(); }

Upvotes: 10

Flask
Flask

Reputation: 4996

<a class="button" href="Cancel" style="left: 55%;" onclick="clearForm();return false;">Cancel</a>

should work

Upvotes: 1

Vijay
Vijay

Reputation: 5433

Change your code as

<a class="button" href="Cancel" onclick="return clearForm()">Cancel</a>

Upvotes: 24

Related Questions