Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Password Confirmation Form Perfect

Danielle Howard
Danielle Howard
4,402 Points

Alternative way of disabling the submit button?

Is this a valid alternative way of disabling the submit button?

function enableSubmitEvent() { if (canSubmit()) { $("#submit").prop("disabled", false); } }

The rest of my code is exactly the same as in the video.

Thanks

2 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

You're on the right track. However, the way you have it written currently would enable the button and since buttons are generally enabled by default this probably would not do anything at all. You still need a way to disable the button when canSubmit() equals false.

A way to do this could be:

function enableSubmitEvent() {
  if (!canSubmit()) {
    $("#submit").prop("disabled", true);
  } else {
    $("#submit").prop("disabled", false);
  }
}

The way shown in the video is obviously the shorter way.

aw man, I am too slow! >.<

Sean T. Unwin
Sean T. Unwin
28,690 Points

Haha! That's happened so many times to me, too.

It is not a true alternative because you are only enabling the submit, but not disabling it. So if the user enters valid info into the form, submit is enabled. If they then change something and the form is no longer valid they can still submit it.

This is a working alternative:

function enableSubmitEvent() { 
  if (canSubmit()) { 
    $("#submit").prop("disabled", false); 
  } else  {
     $("#submit").prop("disabled", true); 
  }
}

There is more code here though, so I would say Andrew's code in the video is the slightly better way of doing it.

Matthias NΓΆrr
Matthias NΓΆrr
8,614 Points

So to clarify

("disabled", false)

just means: submit button is enabled ?!