2

I want to go through all fields of a form and determine if the fields are populated with data. When all fields are given, I'd like a button save to appear.

This is the Fiddle with code, that works (!) ...

This is the same in JavaScript.

The jQuery-Object to iterate through:

formElements = $ 'form input[type="text"], form input[type="number"], form textarea'

The Function:

formFilled = ->
  filled = true
  formElements.each ->
    if $(this).val().length is 0
      filled = false
  filled

The Event-Handler:

formElements.on 'keyup', (keyup) ->
  keyup.preventDefault()
  if formFilled()
    save.show()
  else
    save.hide()

Any suggestions?

5
  • What error are you getting? Commented Nov 14, 2016 at 23:57
  • the save button save never shows up (is hidden all the time) ... Commented Nov 15, 2016 at 0:00
  • There's nothing that's asynchronous here. Does your keyup handler get called at all? Does formElements actually have anything in it? Commented Nov 15, 2016 at 2:35
  • @Timo I see you've thrown a lot of questions up in the last 24 hours on this. Could you create a [JS fiddle|jsfiddle.net/] including your coffeescript and html? We really need to see the whole code to figure this out. And you can insert coffeescript into the fiddle by clicking on the javascript text in the bottom left text area Commented Nov 15, 2016 at 9:22
  • jsfiddle.net/mwmk3ha1/3 Commented Nov 15, 2016 at 11:32

1 Answer 1

1

# I know the answer:

JavaScript validates the number-input fields and accepts only values that are numbers.

I stupidly tested those fields with text input ... silly.

The code is fine. Just the testing was bad. Sorry for wasting your time.

This works:

formInputs = $ 'input[type="text"], input[type="number"], textarea'

formFilled = ->
  filled = true
  formInputs.each ->
    if $(this).val().length is 0
      filled = false
  filled

formInputs.on 'keyup', (keyup) ->
  if formFilled()
    saveButton.show()
  else
    saveButton.hide()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.