user1646859
user1646859

Reputation:

Using click event while button is disabled

I need to check on clicks while the button is disabled is this possible? Or is there any other way to this?

HTML:

<form id="form">
  <input type="submit" id="submit" value="Submit" />
</form>

JS:

$("#form").submit(function (e) {
    e.preventDefault();
    return false;
    $("#submit").on("click", function () {
        alert("Bla");
    });
});

JS Fiddle: http://jsfiddle.net/hjYeR/1/

Upvotes: 0

Views: 216

Answers (3)

Starx
Starx

Reputation: 78981

return statements are the end point in the function, the codes will not proceed ahead of that.

What you can do is simply remove the click event handler from within the submit handler itself.

$("#form").submit(function (e) {

    return false; //e.preventDefault(); is not needed when used return false;

});

$("#submit").on("click", function () {
    alert("Bla");
});

Upvotes: 0

JSP64
JSP64

Reputation: 1462

After you return false; the rest of your function will not run. You can bind your click event before returning false and it should work.

Upvotes: 0

user2417834
user2417834

Reputation:

When you are using preventDefault(), there is no need to use return false.

However, any code after return statement in a function, won't execute.

Also there is no need to attach an event inside another event, write them separately:

$("#form").submit(function (e) {
    e.preventDefault();
});

$("#submit").on("click", function () {
    alert("Bla");
});

jsFiddle Demo

Upvotes: 2

Related Questions