Mark Locklear
Mark Locklear

Reputation: 5325

Disable submit button in coffeescript

I have some date validation that I am doing that works well. In addition to displaying an error message when there is an invalid date range I would like to disable/enable the submit button. Here is my coffeescript...

jQuery ->
  to_date = null
  from_date = null
  $("#to_date, #from_date").bind "change", ->
    from_date = new Date($('#from_date').val())
    to_date = new Date($('#to_date').val())
    if from_date > to_date
      $("#date_error").text("Error! Not a valid date range")
      $("#submit_button").attr("disabled", true)
    else
      $("#date_error").text("")

...and my html when I do view/source...

...
  <div id="submit_button">
    <input name="commit" type="submit" value="Create Orientation" />
  </div>
...

I know my JS is loading cuz my error message is being displayed, no button disabled. Ideas?

Upvotes: 0

Views: 3266

Answers (2)

Flo
Flo

Reputation: 540

You can use .preventDefault();

I didn't try with your code, but you can test this:

  $('#submit_button input').click (e) ->
    e.preventDefault()

Upvotes: 0

Niall Paterson
Niall Paterson

Reputation: 3580

You can't disable a div. You've to disable the actual input. So just do this:

$("#submit_button input").attr("disabled", true)

Upvotes: 1

Related Questions