1

On a new project, I found those lines of codes:

function disableLinkButton() {
    document.getElementById('<%= btnValid.ClientID %>').disabled = "disabled";
    document.getElementById('<%= btnValid.ClientID %>').onclick = returnFalse;
};

function returnFalse() {
    return false;
};

Does the returnFalse method make any sense?

Thanks

2
  • 3
    Partially. Most browsers interpret a return value of the constant false to mean that the default action for an event (a "click" in this case) should not be taken. Older browsers were not so consistent. Commented Oct 20 at 10:28
  • Looks like it's just someone's attempt at shorthand for .onclick = () => false; If the code is quite old, then pre ES6 this would have been .onclick = function() { return false; } - so you can see returnFalse is much shorter if used a lot. Commented Oct 20 at 16:38

1 Answer 1

1

No, that returnFalse() function doesn't really add value in this context. Setting onclick = returnFalse; means when the button is clicked, it just calls a function that returns false.

Since you already disabled the button with .disabled = "disabled", it won’t be clickable anyway. So the extra handler does nothing.

You can simplify using

function disableLinkButton() {
  document.getElementById('<%= btnValid.ClientID %>').disabled = true;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.